Debugging (apparently) random retain loop bug (NSArrayController)
Debugging (apparently) random retain loop bug (NSArrayController)
- Subject: Debugging (apparently) random retain loop bug (NSArrayController)
- From: Keith Blount <email@hidden>
- Date: Thu, 4 Feb 2010 12:21:32 -0800 (PST)
Hello,
For several months now I've been trying to track down a particular bug that can cause a crash in my app, and finally I figured I would cry for help in case there is anything obvious that I can try that I'm not already trying. Here's the situation:
Occasionally, if you close a document (NSDocument subclass) in my app and then open another document (or reopen the same document), the app crashes. I've debugged this to the extent that I now know the cause, at least. What is happening is that sometimes when a document gets closed, a particular NSArrayController subclass instance isn't getting deallocated - but only sometimes. Then, when another document gets loaded, one of the notifications it sends out is picked up by the array controller still in memory which was registered as an observer for that particular notification; the array controller then tries to call out to other parts of its associated document - but of course they no longer exist, and neither should it, so it crashes.
The array controller in question is stored (and instantiated) in the main document .nib (the one named MyDocument.nib by default in new Xcode projects). File's owner is a subclass of NSWindowController. I am aware of certain retain-loop issues with NSArrayController that have been improved since Tiger but are still not 100% fixed to the best of my knowledge, so I'm careful to work around them by not hooking up the content in IB - instead I set and remove content in code.
So, I know the cause of the crash and I know exactly which class isn't getting deallocated; I even have some ideas about how I might approach fixing it. The problem is that it is so random, and doesn't happen very often (then again, sometimes it happens more often than others), so implementing any potential fixes will really be like stabbing in the dark as testing will be impossible. My app is fairly complex so I figured I may need to go through and try doing lots of different things - that is, trying one feature, then closing and reopening a project, then trying another feature and closing and reopening again, and on and on, so that I could narrow down what affected it; the thinking being that there must be one particular thing causing the retain loop. But I just made it crash, launched my app again and then followed the exact same steps - and the array controller deallocated fine the second time around.
So my question is, what is the best approach to track down tricky, elusive memory bugs like this? I already have NSDebugEnabled, NSZombieEnabled, MallocStackLogging and MallocStackLoggingNoCompact set to YES in the arguments pane of my executable's properties. They helped me find out where the crash was happening but not how or where my array controller is being retained.
Has anybody got any good advice (or links to good advice) on how to track down something like this? At the moment I'm stumped because I can't seem to make it reproduceable every time.
Many thanks and all the best,
Keith
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden