Re: Right place to unobserve notifications in a document
Re: Right place to unobserve notifications in a document
- Subject: Re: Right place to unobserve notifications in a document
- From: "Michael Ash" <email@hidden>
- Date: Mon, 18 Aug 2008 12:57:59 -0400
On Mon, Aug 18, 2008 at 11:51 AM, Negm-Awad Amin <email@hidden> wrote:
> But anyway I do not think, that doing something else then memory management
> in -dealloc is good design. Normally I unregister observation in delegate
> methods like -applicationWillTerminate:.
I think you may be a little too strict about this.
First, this really is memory management, it's just not so obvious.
You're removing another object's weak reference to your own object.
The logical place to do this is right before your own object is
deallocated.
Second, it's fine, even good, to clean up arbitrary resources in
-dealloc. There are two things to watch out for, though:
1) You shouldn't clean up *scarce* resources *only* in dealloc. For
example, file descriptors. It's fine to close fds in dealloc. In fact,
you really should do this. But you should *also* provide an explicit
"close" method and use it. The close in dealloc will just function as
a failsafe. This is because your object's lifetime may be much longer
than you want it to be due to things like autorelease pools, so you
want scarce resources to be under explicit control. This is much more
important in a GC environment than in the manual environment, however.
2) You shouldn't clean up *external* resources *only* in dealloc. For
example, if you have an object that keeps a temporary file around,
then only deleting it in dealloc is a bad idea. This is because, as
you're probably aware, there's no guarantee that your objects get
deallocated before your application terminates. But again, it's fine
to do this kind of cleanup in dealloc as a failsafe, just don't have
it be the *only* place it happens.
What you don't want to do is make dealloc part of an RAII (Resources
Acquisition is Initialization) pattern, or do long running
computations there, or things like that. But cleaning up external
references to your object before you disappear is a good thing to do
there.
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