Long story short, I am developing a simple game, and I have a Game
object that keeps track of the current game state, as well as some additional things that are useful/required later on. One of these is a reference to the UIViewController
that loaded the game, which is simply declared as a property on the object:
@interface Game : NSObject {
//ivars
}
@property (nonatomic, retain) myViewController* viewController;
The getters/setters for this property are generated using @synthesize viewController
, and all seems well.
Now there are only two paths through which this property is accessed, when the game is won and when the game is lost. The "game is lost" path works fine. The "game is won" path is causing a bizarre error in which the value of the viewController
pointer seems to get overwritten with garbage.
In order to figure out what is going on, I overrode the getters and setters as follows:
- (myViewController*) viewController {
NSLog(@"Getting");
return viewController;
}
- (void) setViewController:(myViewController*)controller {
NSLog(@"Setting");
viewController = controller;
}
...and set breakpoints inside (I know I'm not retaining the view controller is my setter, but that is not the issue; the view controller remains on the stack and does not actually need to be retained by Game
). Here is what I got in GDB:
//first call to setter
po controller
<myViewController: 0x9208130>
//call to getter on the "game is lost" path
(gdb) po viewController
<myViewController: 0x9208130>
//call to setter, starting a new game from the same view controller
(gdb) po controller
<myViewController: 0x9208130>
//call to getter on the "game is lost" path
(gdb) po viewController
0xbea2222c does not appear to point to a valid object.
(gdb) print viewController
$1 = (myViewController *) 0xbea2222c
So at some point, the value of the pointer was changed from 0x9208130
to 0xbea2222c
, but I can't figure out where. There is no code that assigns into the viewController
property (or its backing ivar) apart from the call to the setter at the start of the game. Yet clearly something is overwriting the value with garbage on the "game is won" path.
Is this possibly a buffer overrun issue? If so what is a good approach for tracking it down?
Edit
I added a second pointer (as an ivar in another class) that I set to the value of game.viewController
at the start of play, and using this pointer for the "game is won" path works. So it appears to me that something is trashing the original pointer. Still no idea what, however.