EXC_BAD_ACCESS when setting a synthesized (saved) property to nil

When I call this line of code, I get bad access (objc_msgsend):

self.currentGameTeam = nil;

Where "currentGameTeam" is defined in the interface for the class named "MCState" as follows:

MNAvailableTeamContext *currentGameTeam;

And I synthesize a property for it:

@property (retain) MNAvailableTeamContext *currentGameTeam;

After installing NSZombieEnabled, the console shows:

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

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

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

, , . , , , . , , MCState 3 , , currentGameTeam, , :

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

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

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

,

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

- currentGameTeam . ?

+3
4

-, currentGameTeam , nil. nil , . EXC_BAD_ACCESS. , NSZombies.

Zombies - , , .

+7

ivar currentGameTeam?

0

, -, currentGameUser, currentGameTeam, . Build and Analyze to , Run with Performance Tool > Leaks.

I’m talking about this since your code looks great, except that you declare stored*and then declare and synthesize the properties for current*, which looks like inconsistency, so there may be other places where similar kinds of close, but different typos may have occurred .

0
source

The last time set to nil should already set this value to nil.

So, next time the nil value should not be harmful. Not sure why the accident.

-2
source

Source: https://habr.com/ru/post/1789653/


All Articles