• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: another n00b question -- how to find a memory leak
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: another n00b question -- how to find a memory leak


  • Subject: Re: another n00b question -- how to find a memory leak
  • From: John Zorko <email@hidden>
  • Date: Wed, 22 Oct 2008 14:21:20 -0700


Shawn,

I'm using KVO in this case as a means to only do certain processing when a thread really exits. I start my Core Audio thread like this:

- (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;
}


... so when the thread stops (or is as close to stopping as I can get -- I figure if it's about to call exit(), that's good enough), this happens:

- (void)threadStopped
{
NSLog(@"*** streamer thread has stopped ***");

[[NSNotificationCenter defaultCenter] removeObserver:self];

MagnatuneAppDelegate *appDelegate = (MagnatuneAppDelegate *) [[UIApplication sharedApplication] delegate];
appDelegate.playbackThreadFinished = true;
}


... and I have this KVO handler:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
						change:(NSDictionary *)change context:(void *)context
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSLog(@"observeValueForKeyPath -- current thread ID: %x, keypath: %s", [NSThread currentThread], [keyPath UTF8String]);

if ([keyPath isEqualToString:@"isPlaying"])
{
.
.
.
}
else if ([keyPath isEqualToString:@"playbackThreadFinished"])
{
if (playbackThreadFinished)
{
// do special stuff that should only happen once the core audio thread has finished
// nothing UI-related goes on here


			[self do_something];
			.
			.
			.
		}

[pool release];
return;
}
else if ([keyPath isEqualToString:@"donePlaying"])
{
.
.
.
}
else if ([keyPath isEqualToString:@"nextSong"])
{
.
.
.
}

[pool release];
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}


So I know the KVO handler gets called on the thread that changed the value. Are you saying that do_something should happen on the main thread i.e. performSelectorOnMainThread:@selector(do_something)? Should I make sure _anything_ that happens in this KVO handler is dispatched to the main thread in that way?


On Oct 22, 2008, at 2:00 PM, Shawn Erickson wrote:

On Wed, Oct 22, 2008 at 1:46 PM, John Zorko <email@hidden> wrote:

Bill,

Alas, I found it 10 minutes after I posted the message *sigh* ... I forgot
the autorelease pool in my override of
-observeValueForKeyPath:ofObject:change:contet: ... the console message is
gone now :-)



Humm that has me concerned... it usually isn't a good idea for KVO to be fired from a secondary thread (since often UI, etc. can be involved and often that isn't safe to be doing from a secondary thread). You likely should be bouncing KVO triggering operations to the main thread.

-Shawn


Regards,

John

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











_______________________________________________

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


  • Follow-Ups:
    • Re: another n00b question -- how to find a memory leak
      • From: "Shawn Erickson" <email@hidden>
References: 
 >another n00b question -- how to find a memory leak (From: John Zorko <email@hidden>)
 >Re: another n00b question -- how to find a memory leak (From: Bill Bumgarner <email@hidden>)
 >Re: another n00b question -- how to find a memory leak (From: John Zorko <email@hidden>)
 >Re: another n00b question -- how to find a memory leak (From: "Shawn Erickson" <email@hidden>)

  • Prev by Date: Re: Get text width outside a view
  • Next by Date: Adding QTMovie to directory wrapper
  • Previous by thread: Re: another n00b question -- how to find a memory leak
  • Next by thread: Re: another n00b question -- how to find a memory leak
  • Index(es):
    • Date
    • Thread