初始化后,Objective-C对象为零 [英] Objective-C object nil after init

查看:49
本文介绍了初始化后,Objective-C对象为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我对Objective-C并不陌生,但是遇到了一个我从未见过的奇怪bug.也许我只犯了一个小错误,您就能看到它.

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

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

但是,如果我与调试器一起跳过此块,则调试器声称buttonsBackground将是一个零指针.但随后在

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

后面加上

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

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

仍然打印行

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

显然意味着它不能为零.有谁知道这怎么可能?

我正在使用LLDB调试器,而不是GDB.属性buttonsBackground声明为

@property(nonatomic, retain)PostCommentButtonsBackground *buttonsBackground;

因此遗失的保留也不是这样.

编辑:好的,我只是看到,我不是唯一一个完全一样的人.另一个人遇到相同的问题调试器lldb说我的对象不是时为零?

无论如何,我只是把这个帖子保留在开放状态,因为在其他人们一直声称的帖子中,开发人员在他的内存管理方面做得很糟糕.在我的帖子中,您实际上可以看到情况并非如此.

解决方案

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

所以,当您遇到错误时,这真的很痛苦.

Okay, I'm NOT new to Objective-C but I encountered a strange bug, that i have never seen before. Maybe I only made a small mistake and you're able to see it.

Within one of my functions I start by creating a new object from a custom view-class in the following way:

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

But if I jump with the debugger over this block, the debugger claims that buttonsBackground would be a nil-pointer. But adding a NSLog right afterwards with

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

still prints the line

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

which clearly means, that it can't be nil. Does anybody have an idea how this can even be possible?

I am using the LLDB Debugger, not the GDB. The property buttonsBackground is declared as

@property(nonatomic, retain)PostCommentButtonsBackground *buttonsBackground;

so a missing retain isn't the case either.

EDIT: Okay, I just saw, that i'm not the only person with the exact same problem. There's another person with the same problem Debugger lldb says my object is nil when is not ?

I'm just leaving this post open anyway, because in the other post people kept claiming, the developer did a bad job with his memory management. In my post you can actually see, that this is not the case.

解决方案

LLDB with the new Xcode 4.3 sometimes is totally lost, and using a NSLog instead of LLDB will show you that your initialized object is correctly initialized and not nil.

So, this is really a pain when you're in front of a bug.

这篇关于初始化后,Objective-C对象为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆