Re: static objects "autoreleased with no pool in place - just leaking"
Re: static objects "autoreleased with no pool in place - just leaking"
- Subject: Re: static objects "autoreleased with no pool in place - just leaking"
- From: "Louis C. Sacha" <email@hidden>
- Date: Fri, 13 Feb 2004 12:11:36 -0800
Hello...
In addition to what other people have said, you should be able to do
something similar in your main.mm file if that would work better for
you. You don't have to use categories or make the variables belong to
a class if you don't want to...
As others have said, the problem is that there is no practical way to
have an autorelease pool before main() begins, so all you sould have
to do is declare the variables seperately from their instantiation.
Building on your hypothetical examples:
/* typed in mail, not tested, author suffering from lack of sleep and
caffeine withdrawal, etc, etc... */
// -------------------------------------------------------
// main.mm
#import <Cocoa/Cocoa.h>
static NSColor *sDefaultLabelColor = nil;
static NSColor *sHighlightLabelColor = nil;
static NSImage *yourCursorImage = nil;
/* ... any other static variables ... */
void instantiateStaticVariables( )
{
NSAutoreleasePool *functionPool = [[NSAutoreleasePool alloc] init];
sDefaultLabelColor = [[NSColor colorWithDeviceRed: 0.0
green:0.0 blue:0.0 alpha:1.0] retain];
sHighlightLabelColor = [[NSColor colorWithDeviceRed: 0.0
green:0.25 blue:0.75 alpha:0.5] retain];
yourCursorImage = [[NSImage imageNamed:@"busy.tif"] retain];
/* ... instantiate any additional static variables ... */
[functionPool release];
}
int main(int argc, const char *argv[])
{
instantiateStaticVariables();
NSAutoreleasePool *mainPool = [[NSAutoreleasePool alloc] init];
/* ... whatever else you needed to do in main, etc ... */
[mainPool release];
return NSApplicationMain(argc, argv);
}
// -------------------------------------------------------
You could also just just put the instantiateStaticVariables() call
inside the autorelease pool calls in main() if you wanted and remove
the autorelease pool stuff in the function code, but that's up to
you... You might also want to note that the objects instantiated for
the static variables were retained, and that the autorelease pool in
main() was released before the NSApplicationMain() call since
otherwise it would just sit there taking up space the entire time the
application is running.
Hope that helps,
Louis
On Feb 12, 2004, at 11:50 PM, j o a r wrote:
Show us the code where the warnings occur (take single steps in GDB
if needed, to be able to pin point the location).
It happens just as the instances are allocated. But you can see for your self!
- in XCode, create a non-document Cocoa app.
- open main.m and paste the code below into it
- save main.m as main.mm (note the double-m extension)
- compile and run while watching the console window
You should see
...class NSDeviceRGBColor autoreleased with no pool in place - just leaking
// -------------------------------------------------------
// main.mm
#import <Cocoa/Cocoa.h>
static NSColor* sDefaultLabelColor = [NSColor colorWithDeviceRed:
0.0 green:0.0 blue:0.0 alpha:1.0];
int main(int argc, const char *argv[])
{
return NSApplicationMain(argc, argv);
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.