 |
 |
autoreleased with no pool in place - just leaking
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status:
Offline
|
|
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
|
|
'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
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status:
Offline
|
|
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
|
|
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
|
|
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 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|