Hello, all ...

I'm experiencing weird issues with addObserver / removeObserver. I log when I add observers, as well as when I try to remove them, but I sometimes (not always) get an exception when I remove an observer:

2008-11-11 10:05:49.939 Magnatune[1382:20b] creating and starting audio engine to play mp3 stream http://he3.magnatune.com/all/02-La%20Febre%20(Uccellini)-Altri%20Stromenti.mp3 2008-11-11 10:05:49.962 Magnatune[1382:20b] audio engine created -- streamer is 90fe00
2008-11-11 10:05:49.970 Magnatune[1382:20b] adding observers ... 11d3b0
2008-11-11 10:05:49.983 Magnatune[1382:ae93] AudioStreamer - startInternal ...
2008-11-11 10:05:50.002 Magnatune[1382:20b] playing stream 1
2008-11-11 10:05:50.534 Magnatune[1382:20b] AudioStreamer -stop() called
2008-11-11 10:05:50.539 Magnatune[1382:20b] AudioStreamer -stop() finished

... the crash:

2008-11-11 10:05:51.538 Magnatune[1382:20b] *** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <MagnatuneAppDelegate 0x11d3b0> for the key path "buffersEmpty" from <AudioStreamer 0x90fe00> because it is not registered as an observer.'

The MagnatuneAppDelegate address (11d3b0) is the same, so i'm adding observers to / removing observers from the same (and only) instance. Why does Cocoa then think that I didn't add the "buffersEmpty" observer when I clearly did?

The crash happens here, in the MagnatuneAppDelegate -stopStream method:

- (void)stopStream
{
        if (streamer)
        {               
                [streamer removeObserver:self forKeyPath:@"buffersEmpty"];
                [streamer stop];
        }
}

... and MagnatuneAppDelegate spawns the streamer thread like this:

- (void)playStream:(UInt32)streamIndexToPlay
{
NSLog(@"playStream -- current thread ID: %x", [NSThread currentThread]);

        .       
        .
        .
        
        if (streamIndexToPlay < [self countOfCurrentSongList])
        {
                self.donePlaying = false;
Song *streamToPlay = [self objectInCurrentSongListAtIndex:streamIndexToPlay];
        
                NSString *address = @"http://he3.magnatune.com/all/";;
                address = [address stringByAppendingString:streamToPlay.mp3];

                .       
                .
                .

NSURL *url = [NSURL URLWithString:[address stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; NSLog(@"creating and starting audio engine to play mp3 stream %s", [[url description] UTF8String]);

                streamer = [[AudioStreamer alloc] initWithURL:url];
                NSLog(@"audio engine created -- streamer is %x", streamer);
                NSLog(@"adding observers ... %x", self);
[streamer addObserver:self forKeyPath:@"isPlaying" options:0 context:nil]; [streamer addObserver:self forKeyPath:@"failed" options:0 context:nil]; [streamer addObserver:self forKeyPath:@"buffersEmpty" options:0 context:nil];
                [streamer start];
        }

        .
        .
        .
}

The AudioStreamer -start method is:

- (void)start
{
streamerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startInternal) object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                                                
  selector:@selector(threadStopped)
                                                                                
  name:NSThreadWillExitNotification
                                                                                
  object:streamerThread];
        [streamerThread start];
        
MagnatuneAppDelegate *appDelegate = (MagnatuneAppDelegate *) [[UIApplication sharedApplication] delegate];
        appDelegate.playbackThreadFinished = false;
}

Regards,

John

Falling You - exploring the beauty of voice and sound
http://www.fallingyou.com











_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to