Re: File descriptors not freed up without a -closeFile call
Re: File descriptors not freed up without a -closeFile call
- Subject: Re: File descriptors not freed up without a -closeFile call
- From: Ben Haller <email@hidden>
- Date: Wed, 23 Jun 2010 06:45:26 -0400
On 23-Jun-10, at 6:34 AM, Jean-Daniel Dupas wrote:
Le 23 juin 2010 à 12:14, Ben Haller a écrit :
So I have a workaround for the problem, but I want to understand
*why* it works. Shouldn't NSPipe close its associated files when
it deallocs? Why should it be necessary to call -closeFile? This
behavior seems to be specifically contradicted by the documentation
on NSPipe, which says (in -fileHandleForReading) "The descriptor
represented by this object is deleted, and the object itself is
automatically deallocated when the receiver is deallocated." Well,
when my NSPipes are deallocated, the file handles are indeed
automatically deallocated (a funny way to put it), but the
descriptor is *not* deleted. Is this a bug? More likely I'm
misunderstanding something; can anybody clarify this for me?
In fact, this is not contradicted by the documentation:
See the "Resource Management" section in "Memory Management
programming guide"
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#/
/apple_ref/doc/uid/20000043-SW6
«You should typically not manage scarce resources such as file
descriptors, network connections, and buffers/caches in a dealloc
method. In particular, you should not design classes such that you
are assuming that dealloc will be invoked when you think it will be
invoked. Invocation of dealloc might be delayed or sidestepped,
either because of a bug or because of application tear-down.
Instead, if you have a class whose instances manage scarce
resources, you should design your application such that you know
when you no longer need the resources and can then tell the instance
to “clean up” at that point. You would typically then release the
instance and dealloc would follow, but you will not suffer
additional problems if it does not.»
That's all well and good, but in point of fact dealloc *is* being
called on the NSFileHandle, and yet the file descriptor is not being
released. I agree that calling -closeFile explicitly when I know I'm
done with it is probably good programming practice for the reasons
above; but it should not be *necessary*, given that the file handles
are in fact being dealloced in a timely manner.
Ben Haller
McGill University
_______________________________________________
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