Re: Update Cocoa object from callback
Re: Update Cocoa object from callback
- Subject: Re: Update Cocoa object from callback
- From: Brian Willoughby <email@hidden>
- Date: Wed, 21 May 2003 03:02:14 -0700
[ The message "_NSAutoreleaseNoPool(): Object 0x17f7ab0 of class
[ NSCFNumber autoreleased with no pool in place - just leaking" is
[ appearing because Cocoa's convenience constructors autorelease
[ the objects they return, so:
[
[ [NSNumber numberWithUnsignedInt:myValue] is exactly the same as
[ [[[NSNumber alloc] initWithUnsignedInt:myValue] autorelease];
[
[ Now, functions executing on your application's main run loop have
[ autorelease pools created for them by the application. Since
[ CoreAudio callbacks often are not called on the main thread
[ (exception: see the CoreAudio.framework property
[ 'kAudioHardwarePropertyRunLoop'), you don't have an
[ autoreleasePool to manage those objects. Try the following code
[ in your listener:
[
[ {
[ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[ [self performSelectorOnMainThread:@selector(updateMyView:)
[ withObject:[NSNumber numberWithUnsignedInt:myValue] waitUntilDone:NO];
[ [pool release];
[ }
Everything stated above is true, but there is another variation.
Autorelease pools can be very inefficient. Any time you know the scope of an
object, you can skip autorelease and manually release the object. I would
suggest the following:
{
NSNumber *number = [[NSNumber alloc] initWithUnsignedInt:myValue];
[self performSelectorOnMainThread:@selector(updateMyView:)
withObject:number waitUntilDone:NO];
[number release];
}
If you look at the documentation for the -performSelector* methods, you'll see
that they retain the object and target for the duration. This means that your
number object will continue to exist until -updateMyView: is done with it.
The only difference between the two blocks above is that the former creates an
unnecessary pool object to keep track of exactly one object. You can keep
track of that one object yourself and save the overhead.
Brian Willoughby
Sound Consulting
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.