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?
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 :
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:
Controller.m:
I stumbled upon this case several times when working with
UITableView
s. I created a private@property (strong) id *yourObjectRetain
and assigned my object to it. An array for multiple objects will also work.