Stop ARC releasing an object while it is idle

2020-04-01 23:07发布

I am having a problem with memory management in objective C. Ive been reading through the Advanced Memory Management Programming Guide but I cannot find a solution to my problem with the possible exception of abandoning ARC altogether and managing the memory manually.

Here is the problem:

I have a Controller class that Ive made, that holds information on what to do at specific times.

The Controller class tells the rest of the app to play a video (for example). The video plays fine. When the video finishes playing, the Controller class knows what to do next.

Unfortunately the Controller class is released and deallocated by ARC almost as soon as the video starts playing. So by the time the video ends, the app calls the Controller class to see what it should do next, and the whole thing crashes. I get an EXC_BAD_ACCESS because the class is no longer in memory.

I get that ARC is releasing my Controller class because after it has told the video to start playing, its not doing anything. But I want to keep hold of that class until I need it again.

I am declaring this class as a property, like so:

@property (strong, nonatomic) Controller * controller;

But despite this, ARC keeps releasing the class as soon as its not doing anything.

EDIT:

Ive moved this property into the App Delegate. But ARC is still releasing it. I cant turn this into a Singleton, as I need the potential to have multiple copies of this class.

How can I stop ARC releasing objects when I dont want it to??

Is it possible to keep an object in memory while its not doing anything?

Is this possible at all? Or should I abandon ARC and just do memory management manually?

标签: objective-c
3条回答
够拽才男人
2楼-- · 2020-04-01 23:50

Your controller is getting dealloc'ed when the detailViewController is dealloc'ed. Hence, you must move the handle of your controller and define in it the any of the following :

  1. MasterViewController or your application's RootViewController OR
  2. AppDelegate OR
  3. Create a singleton as answered by "trojanfoe"
查看更多
一纸荒年 Trace。
3楼-- · 2020-04-01 23:58

Use a singleton pattern so that Controller looks after its own lifetime and exists app-wide. This shared instance will exist from when it's first requested until the app terminates and ARC will not release it arbitrarily.

Controller.h:

@interface Controller : NSObject

+ (Controller *)sharedInstance;

@end

Controller.m:

#import "Controller.h"

static Controller *_instance = nil;
static dispatch_once_t _onceToken = 0;

@implementation Controller

+ (Controller *)sharedInstance {
    dispatch_once(&_onceToken, ^{
        _instance = [[Controller alloc] init];
    });
    return _instance;
}

// You could add this if you want to explicitly destroy the instance:
+ (void)destroy {
    _instance = nil;
    _onceToken = 0;
}
查看更多
小情绪 Triste *
4楼-- · 2020-04-01 23:59

I stumbled upon this case several times when working with UITableViews. I created a private @property (strong) id *yourObjectRetain and assigned my object to it. An array for multiple objects will also work.

查看更多
登录 后发表回答