Release from zero to free memory

In my root view controller, in my didReceiveMemoryWarning method, I look at a couple of data structures (which are stored in a global singleton called DataManager) and drop the hardest things that I have - one or two images, maybe twenty or thirty or more records data.

Now I go through and set them to zero. I also set a boolean flag so that the various view controllers that need this data can easily recognize for a reboot. Thusly:

DataManager *data = [DataManager sharedDataManager]; for (Event *event in data.eventList) { event.image = nil; event.thumbnail = nil; } for (WondrMark *mark in data.wondrMarks) { mark.image = nil; } [DataManager sharedDataManager].cleanedMemory = YES; 

Today I think, though ... and I'm not really sure that everything that allocates memory is really freed up when I do this. Should I use these images instead of release and possibly hit them with new alloc and init when I need them later?

+2
source share
3 answers

Setting the instance variable directly, as mipadi says, does not free the object to which it refers. However, what you do is different from the others: you set the property value of the object. Depending on how this property is declared, this can really release the value it refers to. A property declared retain or copy , rather than assign , receives a synthesized access method ( setImage: method, which converts the .image = syntax), which releases its old value when setting a new one. So, in this case, if your WondrMark property WondrMark declared as retain , setting it to nil through the property, it will automatically undo the old image. If it is assign , you need to release the image before setting the property to nil.

+5
source

Objects are not freed when you set them to nil , so yes, you have to free them.

0
source

You cannot directly pass instances or properties of instances of other objects. These objects themselves are responsible for this.

However, if an object can free its instance variables when setting a new one (for example, with retain parameters), then setting the instance variable to nil will cause the object to return the old value.

0
source

All Articles