Ios 此自动释放使用是否存在任何潜在错误?

Ios 此自动释放使用是否存在任何潜在错误?,ios,uilabel,autorelease,Ios,Uilabel,Autorelease,或者:这种UILabel用法如何可能生成NSMutableDictionary NSInvalidArgumentException 这是一个非ARC iOS应用程序。当showLabel(如下)运行时,我会偶尔(但很少)看到[\uu NSDictionaryM setObject:forKey:]抛出一个错误:NSInvalidArgumentException*setObjectForKey:key不能为零 @property (nonatomic, retain) UILabel *myL

或者:这种UILabel用法如何可能生成NSMutableDictionary NSInvalidArgumentException

这是一个非ARC iOS应用程序。当showLabel(如下)运行时,我会偶尔(但很少)看到[\uu NSDictionaryM setObject:forKey:]抛出一个错误:NSInvalidArgumentException*setObjectForKey:key不能为零

@property (nonatomic, retain) UILabel *myLabel;
@synthesize myLabel = _myLabel;

- (void)showLabel{

if (self.myLabel) {
    return;
}

self.myLabel                        = [[[UILabel alloc] initWithFrame:self.tableView.frame] autorelease];
self.myLabel.textColor              = [UIColor whiteColor];
self.myLabel.shadowColor            = [UIColor blackColor];
self.myLabel.shadowOffset           = CGSizeMake(0, 1);
self.myLabel.textAlignment          = UITextAlignmentCenter;
self.myLabel.text                   = @"blah";
self.myLabel.userInteractionEnabled = YES;
[self.myLabel addGestureRecognizer:[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped:)] autorelease]];
[self.view addSubview:self.myLabel];
[self.view bringSubviewToFront:self.myLabel];

}

- (void)hideLabel {
if (!self.myLabel) {
    return;
}

[self.myLabel removeFromSuperview];
self.myLabel = nil;

}
如果我在[uu\NSDictionaryM setObject:forKey:]上中断,它将在UILabel的setTextColor、setShadow、setShadowOffset和setTextAlignment方法中被调用。我发送的任何值都不应为零,但字典的使用是标签的内部用法,因此我想知道,在发送autorelease后,是否因为其他事件导致autorelease池在我仍然使用方法时耗尽(应用程序中涉及多个外部库),而丢失了对标签的引用,因此,内部词典的使用偶尔会出现错误

这可能解释本例中NSMutableDictionary的NSInvalidArgumentException错误吗

以下是完整的堆栈跟踪:

NSInvalidArgumentException *** setObjectForKey: key cannot be nil

1 libobjc.A.dylib 0x3678c963 objc_exception_throw + 31
2 CoreFoundation 0x378625ef -[__NSDictionaryM setObject:forKey:] + 143
3 MyApp 0x000effab -[TableViewController showLabel] (MainTableViewController.m:222)
4 CoreFoundation 0x37851349 _CFXNotificationPost + 1421
5 Foundation 0x37b2d38f -[NSNotificationCenter postNotificationName:object:userInfo:] + 71
6 MyApp 0x000d9141 -[Event setDictionary:] (Event.m:123)

我可以有把握地说,这与自动释放无关。您的属性有一个retain,并且您在存储变量时正确使用了该属性,因此您的retain计数器变为+1(alloc)+1(属性设置器retain),最后变为-1(自动释放)

您的问题可能在其他地方,但如果没有更多的代码,我无法解决它,抱歉

编辑:如果你想玩超级安全游戏(我真的建议你这么做),你可以这样做:

self.myLabel = [[UILabel alloc] initWithFrame:self.tableView.frame];
// configure
[self.myLabel release];
手势识别器也是如此


试试看你是否仍然会崩溃

我添加了堆栈跟踪,如果有帮助的话。其他地方有很多字典用法(包括通过NSNotification触发此方法调用的事件),但在跟踪中列出的showLabel方法中,标签配置方法是唯一显示字典用法的方法。如果没有,请转到XCode上的断点选项卡,单击添加
(+)
按钮,选择左下角的
添加异常断点…
并单击
完成
。它将准确地告诉您它在哪一行崩溃,以及此时控制器的状态。代码看起来很好,所以我不知道它可能意味着什么。对于我来说,在调试器中捕捉它太少了。到目前为止,这只发生在野外。谢谢你的帮助!新年快乐:)很抱歉我没能帮你,新年快乐!我想一个相关的问题是:如果错误实际上没有在showLabel中发生,那么这个堆栈跟踪是否可能发生?例如,如果在调用showLabel之前,setDictionary中确实存在问题?