在init之后,Objective-C对象为零

时间:2012-03-02 12:21:13

标签: objective-c lldb

好吧,我不是Objective-C的新手,但我遇到了一个奇怪的错误,这是我以前从未见过的。也许我只犯了一个小错误,你就能看到它。

在我的一个函数中,我首先按以下方式从自定义视图类创建一个新对象:

[buttonsBackground removeFromSuperview];
self.buttonsBackground = [[[PostCommentButtonsBackground alloc] initWithFrame:self.contentView.bounds] autorelease];
buttonsBackground.delegate = self;

但是如果我在调试器上跳过这个块,调试器会声称buttonsBackground将是一个零指针。但是之后用

添加NSLog
NSLog(@"%@",self.buttonsBackground);

仍会打印

<PostCommentButtonsBackground: 0x7bd27a0; frame = (0 0; 320 82); layer = <CALayer: 0x6e688b0>>

这显然意味着它不能为零。有没有人知道这怎么可能呢?

我使用的是LLDB调试器,而不是GDB。属性buttonsBackground声明为

@property(nonatomic, retain)PostCommentButtonsBackground *buttonsBackground;

所以缺少保留也不是这样。

编辑:好的,我刚看到,我不是唯一一个有完全相同问题的人。还有另一个人有同样的问题Debugger lldb says my object is nil when is not ?

我只是打开这个帖子,因为在人们不断声称的其他帖子中,开发人员在他的记忆管理方面做得不好。在我的帖子中你可以看到,事实并非如此。

1 个答案:

答案 0 :(得分:3)

使用新Xcode 4.3的LLDB有时会完全丢失,使用NSLog而不是LLDB会向您显示初始化对象已正确初始化而不是nil

所以,当你遇到一个错误时,这真的很痛苦。