AppDelegate instance variable getting zombie
In the AppDelegate I have a user variable in order to be accessible throughout the application. Tha AppDelegate code is the following:
@interface MyAppDelegate : NSObject <UIApplicationDelegate, ...> {
@private User *_appUser;
...
}
- (void)setUser:(User *)value;
- (User *)appUser;
- (void)setUser:(User *)value
{
@synchronized(self)
{
if(_appUser != value)
{
[_appUser release];
_appUser = [value retain];
[NSUserDefaultsManager SaveUser:_appUser];
}
}
}
- (User *)appUser
{
if(_appUser == nil)
return [NSUserDefaultsManager GetUser];
else
return _appUser;
}
The first time, when the user registers, I s开发者_如何学编程et the user via a ViewController by doing
[((MyAppDelegate *)[[UIApplication sharedApplication] delegate]) setUser:user];
The thing is that when I need to get the user by doing
User *user = [((MyAppDelegate *)[[UIApplication sharedApplication] delegate]) appUser];
in the appUser method of the AppDelegate, the _appUser ivar is Zombie. Any workaround for that? Thank you in advance.
Is your getter method the culprit? You are returning the _appUser without the usual retain/autorelease pattern. Can you resolve the problem by making your getter method:
- (User *)appUser
{
if(_appUser == nil)
return [NSUserDefaultsManager GetUser];
else
return [[_appUser retain] autorelease];
}
or...
- (User *)appUser
{
if(_appUser == nil)
_appUser = [[NSUserDefaultsManager GetUser] retain];
return [[_appUser retain] autorelease];
}
精彩评论