views:

132

answers:

4

So I am creating an class that does an animation. There is a timer delay. So I will instantiate this class. Have it do its thing then have a timerFinished type of delegate to continue on.

This question comes from my sheltered garbage collection career.

What is the best way to release this object?

Do I have it release itself when it finishes that task (not even sure that this is possible). Do I have the object that implements the delegate release it?

Having trouble wrapping my mind around the best way to do this.

A: 

Yes, when you're done with the animation, you can send the animation class instance a [self release] message. Just be sure you're really done with the animation class when you send the message.

Mike
Sending a class a `release` message won't do anything, since `release` is an instance method, not a class method. Moreover, object suicide is an anti-pattern; better for something to own the animation, and release and relinquish the animation object when the animation finishes (or, for things other than animations, is aborted). This aids debugging and leak-hunting, besides just being cleaner.
Peter Hosey
what bugs me about this idea is what happens when I have multiple objects listening to the same isFinished event? Then it isn't so clean to have the listener to the isFinished event release the object.What I really need to know is when all the isFinished event handlers have fired. The only thing that seems to know when that happens is the object itself...Am I thinking about this wrong?
Mel
Mel: If multiple objects need to know that the animation finished, they should listen for an NSNotification that your animation object posts. Notifying multiple objects that something has happened is the purpose of NSNotification and NSNotificationCenter.
Peter Hosey
+1  A: 

Generally, if you are not sure when or where to release your object you can call autorelease on it when initializing. Like this:

NSObject *anObject = [[[NSObject *anObject] init] autorelease];
ff10
If I do this wont my class disappear the next time the autorelease pool it called possibly before the animation is finished?
Mel
Yes. You would normally provide an animation delegate callback that gets called when the animation is finished, and release what you want to release there.
Kendall Helmstetter Gelner
+1  A: 

You should have your object post a notification when it's finished or send a message to its delegate, and the listening object or delegate can handle cleanup of the object.

Preston
+1  A: 

FWIW, I've found it rather dangerous to have objects that retain/release themselves, for the simple reason that you may have objects hanging around in the background that you don't know about anymore. Even worse, if they message another object, they may be retaining views or controllers you've long closed.

Also, this pattern doesn't work in the garbage collector on MacOS.

So, I'm currently trying to re-educate myself to keep a global NSArray for each class that may have such objects floating around. That way, during debugging I have a place where I can easily see these objects, and I can cancel them from dealloc by e.g. having a "cancelAllConnectionsWithDelegate: self" method.

uliwitness