问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/9533189/objective-c-object-nil-after-init