Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > autoreleased with no pool in place - just leaking

autoreleased with no pool in place - just leaking
Thread Tools
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
May 30, 2003, 01:56 AM
 
In my latest Cocoa experiment, I managed to detach a thread but then get the following console message:


2003-05-30 01:46:17.654 ScratchWork[9872] *** _NSAutoreleaseNoPool(): Object 0xa07e66a4 of class NSCFString autoreleased with no pool in place - just leaking



I don't know why I'm getting it since I'm releasing the object that I create. Below is the relevant code:


Code:
- (IBAction)newThread:(id)sender { [NSThread detachNewThreadSelector:@selector(doThis:) toTarget:self withObject:nil]; } - (void)doThis:(id)ignore { NSCalendarDate *now; now = [NSCalendarDate calendarDate]; [myTextField setObjectValue:now]; [now release]; }
The code produces the expected results but the message bothers me.

Thanks!
(Last edited by DaGuy; May 30, 2003 at 02:08 AM. )
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
May 30, 2003, 02:26 AM
 
'now' is set to be autoreleased when you create it using [NSCalendarDate calendarDate]. It then gets retained when you add it to myTextField, making its retainCount >=2. You then manually release it, dropping its retainCount to >=1. But, it's still set to be autoreleased from when it was created, and you don't have an autorelease pool present.

If you did, it would be dealloced when the pool was released, and myTextField would contain a stale pointer.

Fix by creating an NSAutoreleasePool at the beginning of doThis and releasing it at the end. For extra safety, use an NSException catcher so that if anything throws, your NSAutoreleasePool is still released before the thread is terminated.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
May 30, 2003, 05:46 AM
 
Originally posted by DaGuy:
In my latest Cocoa experiment, I managed to detach a thread but then get the following console message:


2003-05-30 01:46:17.654 ScratchWork[9872] *** _NSAutoreleaseNoPool(): Object 0xa07e66a4 of class NSCFString autoreleased with no pool in place - just leaking



I don't know why I'm getting it since I'm releasing the object that I create. Below is the relevant code:

<snip>
You need to create an NSAutoreleasePool for every thread you create.

If you're writing threaded code you'll want to read and understand this documentation:
http://developer.apple.com/techpubs/...ing/index.html

In particular you'll want to read this, which mentions your Autorelease pool problem:
http://developer.apple.com/techpubs/...detaching.html

- proton
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
May 30, 2003, 09:49 AM
 
Great! Many thanks for your assistance.

Dang Cocoa books... they should've said that up front. But what fun would it be if it were all contained in some book?

     
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Jun 2, 2003, 04:41 AM
 
Yeah, objects returned by class methods are usually always autoreleased. There shouldn't be any need to release them yourself; in fact, if you want to keep them around (e.g., as instance variables) you'll need to retain them yourself.
[Wevah setPostCount:[Wevah postCount] + 1];
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jun 2, 2003, 10:48 AM
 
Objects returned by any method are usually autoreleased, in fact, so if you want to use Foundation at all, you're probably going to need an autorelease pool
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:37 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2