Re: dealloc for cleanup versus freeing memory
Re: dealloc for cleanup versus freeing memory
- Subject: Re: dealloc for cleanup versus freeing memory
- From: Uli Kusterer <email@hidden>
- Date: Tue, 7 Aug 2007 00:07:01 +0200
On 06.08.2007, at 20:42, John Stiles wrote:
If your object is designed to hold a reference to a "scarce
resource" like a file handle, and it's being retained by three
different points in the code, it seems like a bad idea to me for
one point in the code to decide "OK, we're done now, let's wrap it
up" and just close the file. What about the other two places in the
code that were using the object? If they still hold a retain on the
object, they have every reason to expect a working file handle, right?
If you want to say that retain counts are a bad idea in general for
scarce resources, OK, but that's a much broader argument and has
very different ramifications to me.
I don't think the point is that retain counts are bad for scarce
resources. Rather, autorelease is bad. So, you should probably have
your own reference count for whatever scarce resources you have,
which is retained and released when needed, but which doesn't support
autorelease-like semantics, and thus isn't subject to the vagaries of
when a pool is flushed.
What vagaries? Well, judging from behaviour I observed
occasionally, NSApplication's main run loop seems to have been
implemented roughly like (pseudocode with made-up function names):
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
while( [NSApp isRunning] )
{
NSEvent * evt = NXBlockUntilEventAvailable();
[pool release];
pool = [[NSAutoreleasePool alloc] init];
NXDispatchEvent( evt );
}
[pool release];
So since the outermost pool doesn't get cleaned up until
NXBlockUntilEventAvailable() has returned, if your app is in the
background and there are no events for it, your object may be hanging
around for several minutes.
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
_______________________________________________
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