Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Singletons and Memory Management

I'm certain that I'm missing some fundamental understanding of iOS memory management and, despite lots of reading and searching, I'm still not getting it.

I use a singleton in my app that has info about the user currently signed into it, info accessed from multiple view controllers, etc. It has multiple ivars that are get and set throughout the app. They're declared and propertied in the .h file like so:

NSString *myString;

and are made retained like so:

@property (non atomic, retain) NSString *myString;

and synththesized in the implementation.

I get and set their values in methods in the singleton like this:

myString = @"value";

and

methodLocalString = myString;

In other places I include the singleton -- call it CurrentUser -- I import it:

#import "CurrentUser.h"     

Outside of the singleton I get and set it like this:

[CurrentUser sharedCurrentUser].myString = @"Bob";

and

myOutsideString = [CurrentUser sharedCurrentUser].myString;

Most of the time this works great, with the values appropriately persisted from one getting or setting to another. The trouble is that sometimes when I get them that way I find that they've been released (crashing the app), which NSZombieEnabled thankfully tells me.

What I don't get is how his can happen. I thought the singleton was never released, and that therefor retained properties of the singleton would never be released. I'll note that the problem seems to be more common with non-real-object properties like NSDate and definitely-not-object properties like int and BOOL which can't be retained, but it happens with object properties as well.

What am I ignorant about here? And thanks for your patience.

like image 232
Matthew Frederick Avatar asked Nov 19 '10 04:11

Matthew Frederick


1 Answers

Your problem is:

I get and set their values in methods in the singleton like this:

myString = @"value";

When you assign directly to the iVar, instead of using the property syntax (self.myString = @"value"), you are bypassing the synthesized setter method, which means that the retain never happens.

Properties aren't magic. They're just a bit of syntactic sugar for the "." access, and the ability to have synthesized getter/setter methods to save you the tedium of writing your own.

self.myString = @"value";

is just shorthand for

[self setMyString:@"value"];

The synthesized setMyString method will do something like:

if (myString != newValue) {
    [myString release];
    myString = [newValue retain];
}

(assuming retain option on the @synthesize)

like image 59
David Gelhar Avatar answered Oct 08 '22 21:10

David Gelhar