Re: NSDocument reference held by NSSavePanel?
Re: NSDocument reference held by NSSavePanel?
- Subject: Re: NSDocument reference held by NSSavePanel?
- From: Michael Ash <email@hidden>
- Date: Mon, 13 Apr 2009 11:08:59 -0400
On Sun, Apr 12, 2009 at 11:33 PM, Quincey Morris
<email@hidden> wrote:
> Here's something strange. I have a NSDocument-based GC app whose data model
> contains an object that uses a resource. (The resource is actually exclusive
> access to a MMC-controlled device, but that's not really relevant.) The
> object has a finalize method in which the resource is released.
The answer here is simple: don't do this.
Even if all of your references get cleared, you have no guarantee that
-finalize will be called in any sort of timely fashion. Garbage
collection is not deterministic. Usually your object will be finalized
within some seconds of having the last strong reference to it go away.
Can you really withstand having that exclusive resource be occupied
for seconds after your document closes? If the GC decides not to
collect that object for a much longer period of time, say, six years,
which it is perfectly within its rights to do, does that work for you?
-finalize should do two things. First, it should clean up any memory
allocations that aren't managed by the collector, like malloc blocks.
Second, it should clean up non-memory resources *as a fail-safe*. It
should close file descriptors, release shared locks, what have you,
but only to ensure that they weren't forgotten. Any object which holds
an external resource like this should have an explicit invalidation
method to release it in a timely fashion, because the collector may
not be timely at all.
> This all works most of the time. The object is created (and therefore
> acquires its resource) when a new document is being created, or when an
> existing document is being opened. When the document window is closed,
> everything gets garbage collected, the finalize method is called, and the
> resource is released.
>
> However, if I close the document window after using the Save As dialog
> (either for the initial save of a new document, or for a save-as of an
> existing document), the resource is NOT released, because the finalize
> method isn't being called, because there's still a reference to that object.
>
> Here's what the debugger says about that reference:
[snip]
Looks like a bug to me. If you can get multiple such documents to leak
then even more so. One at a time, that's not a big problem, but it
still seems like Cocoa ought to be cleaning this stuff up. Still, fix
the dependency on -finalize as I discussed above, and you won't have
to worry about it (much).
Mike
_______________________________________________
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