views:

45

answers:

1

Run into an interesting problem and not sure how to manage it.

What would be the best way to handle the AVAudioPlayer callback method, audioPlayerDidFinishPlaying, if, for example, an instance of AVAudioPlayer is created with a callback for when the audio has finished but the object containing the AVAudioPlayer (and callback) is deallocated before the audio finishes?

This is my current situation where the app crashes consistently as a now non-existant callback method is trying to be called.

Thinking about it, the answer would probably be relevant across the board of callbacks. There must be some way to either destroy or cancel callback methods.

A: 

You have to keep track of everything you've registered for "callbacks" from and un-set them in -dealloc, e.g.

-(void)dealloc
{
  // If you've registered for notifications, do something like this:
  [[NSNotificationCenter defaultCenter] removeObserver:self];

  // If you're the delegate of something, do something like this:
  self.audioPlayer.delegate = nil;
  self.audioPlayer = nil;

  [super dealloc];
}

There's no magic method that searches everything ever for any pointer that looks like it might be a pointer to you and zeroes it. There are loads of problems with that approach (not limited to being slooooow...).

tc.