Ugh, how I hate it. That feeling that you are struggling to fix an issue with your code, and you know you have battled with it before on a different project. Of course you can't remember what the fix was, and the two code bases are too different for easy compare. In the Netherlands, we have a saying: "A donkey will not stumble on the same stone twice." Meaning that if you do, you are dumber than a donkey. Here is the measure I take to make sure I do not get hit a third time: I blog about it for future reference.
My issue involves closing the window of an OpenGL app under Mac OSX by clicking the red window close button. My app would then crash in the OpenGL drawing (triggered by a CADisplayLink.) Stopping a display link is a bit of a mess, as I always seem to be to late with it. Let's see the hooks where I can close down the display link:
My AppDelegate gets callbacks about OSX's intention to close down. We have a lot of these callbacks going on, as there are:
-(void)applicationWillResignActive:(NSNotification *)notification -(void)applicationWillTerminate:(NSNotification *)notification -(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender -(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
Astoundingly none would be fast enough to close down the display link before resources get torn down. It turns out only this one is called early enough:
-(BOOL)windowShouldClose:(id)sender
However, my AppDelegate was not getting called with this. It turns out I had not made my app delegate the delegate for my window:
[ window setDelegate:self ];
This requires the AppDelegate to be a NSWindowDelegate:
@interface DigAppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate>