Unable to explain how a pointer value gets modified in cocoa
I ran into a bug I have trouble explaining. Now that I found the bug, I can certainly fix it but I would like to understand how such a thing could even happen.
My code:
NSLog(@"1 - self.nextPlayerButton = %@",self.nextPlayerButton);
NSLog(@"[self.view setUserInteractionEnabled:TRUE] with self.view=%@",self.view);
[self.view setUserInteractionEnabled:TRUE];
NSLog(@"2 - self.nextPlayerButton = %@",self.nextPlayerButton);
where nextPlayerButton is defined as:
@property (retain) IBOutlet UIBarButtonItem *nextPlayerButton;
The log:
2011-05-15 15:23:05.245 Melimemo[1261:207] 1 - self.nextPlayerButton = <UIBarButtonItem: 0x4b668c0>
2011-05-15 15:23:05.248 Melimemo[1261:207] [self.view setUserInteractionEnabled:TRUE] with self.view=<UIView: 0x4b69750; frame = (0 0; 320 460); autoresize = W+H; layer = <CALayer: 0x4b54290>>
2011-05-15 15:23:05.249 Melimemo[1261:207] 2 - self.nextPlayerButton = <UIBarButtonItem: 0x4b65880>
As you can see, self.nextPlayerButton points to another object the second time around.
The source of my problem is that self.view is actually not properly defined: I initialize an instance of the object in which the code below runs and don't define what view is. Still, how could invoking setUserInteractionEnabled on anything result in modifying the pointer value of self.nextPlayerButton?开发者_运维知识库 Even the beginning of a theory would help.
More likely is that the self.view call is causing loadView to be invoked, and your button to be set a second time to a new button.
Try implementing the setter for nextPlayerButton like this:
- (void)setNextPlayerButton:(UIButton *)button {
if (button != nextPlayerButton) {
[nextPlayerButton release];
nextPlayerButton = [button retain];
}
}
Then put a break point in there and run your app. The break point will probably be hit twice. You can look at the stack trace to see why it's getting hit the unexpected time.
精彩评论