Thanks for the advice, gentlemen. I already had a -cleanUp method being called from -windowWillClose: within the NSDocument (NSPersistentDocument, actually), so I looked more carefully at how that particular view controller is torn down. I made some changes such that now, in the document's cleanUp, I call -tearDown on the view controller. It in turn calls -tearDown on its builder object (which uses a timer to repeatedly refresh the model objects). Currently I can't get the view controller and builder to dealloc before the document, which results in crashes:
2012-09-06 05:41:19.889 TeXnicle[2122:303] Dealloc <TeXProjectDocument: 0x105058f20> 2012-09-06 05:41:19.902 TeXnicle[2122:303] Dealloc <TPOutlineBuilder: 0x105871610> 2012-09-06 05:41:19.902 TeXnicle[2122:303] Dealloc <TPProjectOutlineViewController: 0x10585a1d0> Seems I'm doing something wrong. So, to recap, in the NSDocument I do: - (void)windowWillClose:(NSNotification *)notification { [self cleanUp]; } - (void) cleanUp { NSLog(@"Clean up..."); // outline view controller [self.outlineViewController tearDown]; self.outlineViewController = nil; // and lots of other stuff } then in the view controller I do: - (void) tearDown { NSLog(@"Outline view controller tearDown"); [self.view removeFromSuperview]; self.delegate = nil; self.outlineView.delegate = nil; self.outlineView.dataSource = nil; [self.outlineBuilder tearDown]; self.outlineBuilder = nil; } and in the builder I do - (void) tearDown { [self stopObserving]; [[NSNotificationCenter defaultCenter] removeObserver:self]; [self stopTimer]; self.delegate = nil; dispatch_release(queue); } - (void) stopTimer { if (self.timer) { [self.sections removeAllObjects]; [self.timer invalidate]; self.timer = nil; } } I wonder if I'm coming a cropper because of the timer. I have in the back of my mind a memory about timers retaining their target, but I think I handle that in the accepted way by calling -invalidate on the timer. Maybe I shouldn't care about the order in which my dealloc messages appear? With this new scheme described above, I've been able to open and close a document many 10's of times without a crash. But somehow I have a nagging doubt that I've really nailed this down. Any further advice is greatly appreciated. I've spent a few weeks on and off trying to fix this particular class of crash, and I don't feel I'm much closer to the solution. Best wishes, Martin On 6, Sep, 2012, at 03:01 AM, Graham Cox <graham....@bigpond.com> wrote: > > On 06/09/2012, at 10:44 AM, Jerry Krinock <je...@ieee.org> wrote: > >> Regarding the indication, I've yet to find a single hook in Cocoa which >> gives me a reliable early warning that a document is closing. > > > If your document only has a single window, you could use: > > - (void) windowWillClose:(NSNotification*) notification; > > I've found this a reliable place (in fact the ONLY reliable place) to perform > tear-down of KVO, etc. Of course if you support multiple windows of the same > document that might need a bit of care to make work. > > > --Graham > _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com