小编Mat*_*att的帖子

将合成(保留)属性设置为nil时的EXC_BAD_ACCESS

调用这行代码时,我得到一个错误的访问(objc_msgsend):

self.currentGameTeam = nil;
Run Code Online (Sandbox Code Playgroud)

其中"currentGameTeam"在接口中为名为"MCState"的类定义为:

MNAvailableTeamContext *currentGameTeam;
Run Code Online (Sandbox Code Playgroud)

我为它合成了一个属性:

@property (retain) MNAvailableTeamContext *currentGameTeam;
Run Code Online (Sandbox Code Playgroud)

设置NSZombieEnabled后,控制台显示:

*** -[MNAvailableTeamContext release]: message sent to deallocated instance 0x5b3eba0
Run Code Online (Sandbox Code Playgroud)

调试器跟踪显示它来自合成的setter代码:

#3  0x0001fa96 in -[MCState setCurrentGameTeam:] at MCState.m:44
Run Code Online (Sandbox Code Playgroud)

我已经看了几个其他问题和线程,我找不到适用于我的案例的答案.我不明白为什么如果我合成了属性并且我将它设置为nil会有一个糟糕的访问.特别奇怪的是MCState中至少有3个其他属性的定义和使用方式与currentGameTeam完全相同,唯一的区别是它们是不同的类型:

MNUserContext *storedUser;
MNActiveGameContext *storedGame;
MNAvailableUserContext *storedGameUser;
MNAvailableTeamContext *storedGameTeam;
Run Code Online (Sandbox Code Playgroud)

@property (retain) MNUserContext *currentUser;
@property (retain) MNActiveGameContext *currentGame;
@property (retain) MNAvailableUserContext *currentGameUser;
@property (retain) MNAvailableTeamContext *currentGameTeam;
Run Code Online (Sandbox Code Playgroud)

@synthesize currentUser;
@synthesize currentGame;
@synthesize currentGameUser;
@synthesize currentGameTeam;
Run Code Online (Sandbox Code Playgroud)

最后

self.currentUser = userContext;
self.currentGame = nil;
self.currentGameUser = nil;
self.currentGameTeam = nil; // Error …
Run Code Online (Sandbox Code Playgroud)

iphone null properties exc-bad-access objective-c

3
推荐指数
1
解决办法
3716
查看次数

标签 统计

exc-bad-access ×1

iphone ×1

null ×1

objective-c ×1

properties ×1