Re: globals and memory leaks
Re: globals and memory leaks
- Subject: Re: globals and memory leaks
- From: publiclook <email@hidden>
- Date: Sun, 20 Apr 2003 23:18:34 -0400
It is almost OK to do what you are doing. You need the following:
@implementation MyClass
+ (void)initialize
{
if(self == NSClassFromString(@"MYClass"))
{
someImage= [[NSImage imageNamed: @"Some Image"] retain];
}
}
You need the if statement because due to the semantics of +initialize,
it will be called once the first time MyClass is encountered AND it
will be called again for every subclass of MyClass encountered.
Without the if statement, if you have five subclasses of MyClass, you
will potentially end up with 6 copies of the image. However, I suspect
that +imageNamed: returns the same cached instance over and over again
so maybe it is alright to skip the if statement in this case...
On Sunday, April 20, 2003, at 04:43 PM, Francisco Tolmasky wrote:
In my program I frequently do something like this:
static NSImage * someImage;
@implementation myClass
+ (void)initialize
{
someImage= [[NSImage imageNamed: @"Some Image"] retain];
}
//...
@end
Firstly, is this the correct way to do this. Secondly, if it is,
should I be worried about memory leaks, do I have to release this
image when the program terminates?
I also do this with my shared Instances, like this:
Prefernces * sharedPrefs;
@implementation Preferences
+ (void)sharedInstance
{
if(!sharedPrefs) sharedPrefs= [[Preferences alloc] init];
return sharedPrefs;
}
Do I ever release sharedPrefs??
Thank you in advance,
Francisco Tolmasky
email@hidden
http://users.adelphia.net/~ftolmasky
_______________________________________________
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.