开发者

EXC_BAD_ACCESS when setting synthesized (retained) property to nil

I get a bad access (objc_msgsend) when calling this line of code:

self.currentGameTeam = nil;

Where "currentGameTeam" is defined in the interface for a class called "MCState" as:

MNAvailableTeamContext *currentGameTeam;

And I synthesize a property for it:

@property (retain) MNAvailableTeamContext *currentGameTeam;

After setting NSZombieEnabled the console shows:

*** -[MNAvailableTeamContext release]: message sent to deallocated instance 0x5b3eba0

And the debugger trace shows that it comes from within the synthesized setter code:

#3  0x0001fa96 in -[MCState setCurrentGameTeam:] at MCState.m:44

I've looked at several other questions and thread开发者_如何学Cs and I can't find an answer that applies to my case. I don't understand why there would be a bad access if I've synthesized the property and I'm setting it to nil. What is especially odd is that there are at least 3 other properties in MCState which are defined and used in the exact same way as currentGameTeam, with the only difference being that they are different types:

MNUserContext *storedUser;
MNActiveGameContext *storedGame;
MNAvailableUserContext *storedGameUser;
MNAvailableTeamContext *storedGameTeam;

and

@property (retain) MNUserContext *currentUser;
@property (retain) MNActiveGameContext *currentGame;
@property (retain) MNAvailableUserContext *currentGameUser;
@property (retain) MNAvailableTeamContext *currentGameTeam;

and

@synthesize currentUser;
@synthesize currentGame;
@synthesize currentGameUser;
@synthesize currentGameTeam;

finally

self.currentUser = userContext;
self.currentGame = nil;
self.currentGameUser = nil;
self.currentGameTeam = nil; // Error occurs here

The rest of these all act normally - only currentGameTeam gives me trouble. Any ideas?


Somewhere, currentGameTeam is being released before you attempt to set it to nil. Setting a retained property to nil implies a release to be called. Calling release on an object that has already been released will result in a EXC_BAD_ACCESS. This is confirmed with your running with NSZombies enabled.

You can run with Instruments with the Zombies tool - it will give you more detail about all of the retains, releases and autoreleases leading up to the zombie call.


Do you have the ivar named currentGameTeam ?


I've had some bugs like this an usually tracked them down to a typo someplace where I meant to type currentGameUser but have typed currentGameTeam so I end up double-releasing one of the objects and not releasing another. I would try doing a Build and Analyze to and if that doesn't find anything try Run with Performance Tool > Leaks.

I say this since your code looks fine except you declare stored* and then declare and synthesize properties for current*, which looks like an inconsistency so there may be other places where this kinds of close-but-different typos may have occurred.


Last time set to nil should already set that to nil.

So the next time set to nil should not harmful. Not sure why crash.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜