dealloc and scarce resources
dealloc and scarce resources
- Subject: dealloc and scarce resources
- From: James Merkel <email@hidden>
- Date: Wed, 29 Jun 2011 17:43:10 -0700
In another thread, someone referenced the Memory Management
Programming Guide:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/
MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/
TP40004447-1000922-BABBFBGJ
In the Guide it says:
"You should typically not manage scarce resources such as file
descriptors, network connections, and buffers or caches in a dealloc
method. In particular, you should not design classes so 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."
In my code I close a file in the dealloc method -- so I guess that's a
file descriptor. I opened the file in the init method, so it seemed
logical to close it in the dealloc method.
Also, not to be facetious, if I have a bug in my code, wouldn't I fix
it? Granted, at application tear-down, if the file is still open, it
won't be closed because dealloc won't be called. But again that comes
under the heading of a bug in the code. So I don't understand this
injunction.
Jim Merkel_______________________________________________
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