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: Rainer Brockerhoff <email@hidden>
- Date: Fri, 13 Feb 2004 11:15:38 -0200
At 04:48 -0800 13/02/2004, email@hidden wrote:
>
From: m <email@hidden>
>
Date: Fri, 13 Feb 2004 04:05:32 -0800
>
>
On Feb 13, 2004, at 12:45 AM, j o a r wrote:
>
>
> In an OO context you would not have things like this dangling around,
>
> but would instead associate them with a class.
>
>
...
>
In the meantime, your suggestion (which I've moved to for now) requires
>
moving from the conveniently concise and compact:
>
>
static NSColor* kColor1 = [NSColor colorWithDeviceRed: 0.6 green:0.6
>
blue:0.6 alpha:1.0];
>
static NSColor* kColor2 = [NSColor colorWithDeviceRed: 0.6 green:0.6
>
blue:0.4 alpha:1.0];
>
static NSColor* kColor3 = [NSColor colorWithDeviceRed: 0.3 green:0.6
>
blue:0.6 alpha:1.0];
>
static NSColor* kColor4 = [NSColor colorWithDeviceRed: 0.2 green:0.6
>
blue:0.8 alpha:1.0];
>
[...]
>
static NSColor* kColorN = [NSColor colorWithDeviceRed: 0.5 green:0.6
>
blue:0.6 alpha:1.0];
>
>
to the annoyingly verbose, distributed, and easier to screw up/harder
>
to maintain:
>
>
@interface NSColor(MyColorAdditions)
>
>
+ (NSColor*) color1;
>
+ (NSColor*) color2;
>
+ (NSColor*) color3;
>
+ (NSColor*) color4;
>
[...]
>
+ (NSColor*) colorN;
>
>
@end
>
>
@implementation NSColor(MYColorAdditions)
>
>
static NSColor* sColor1;
>
static NSColor* sColor2;
>
static NSColor* sColor3;
>
static NSColor* sColor4;
>
[...]
>
static NSColor* sColorN;
>
>
+ (NSColor*) color1
>
{
>
if (!sColor1)
>
{
>
sColor1 = [[NSColor colorWithDeviceRed: 0.6 green:0.6
>
blue:0.6 alpha:1.0]retain];
>
}
>
return sColor1;
>
}
>
>
[...]
>
>
+ (NSColor*) colorN
>
{
>
if (!sColorN)
>
{
>
sColorN = [[NSColor colorWithDeviceRed: 0.5 green:0.6
>
blue:0.6 alpha:1.0]retain];
>
}
>
return sColorN;
>
}
While purists may frown upon global variables, I usually feel it's perfectly acceptable to do this:
static NSColor* kColor1 = nil;
static NSColor* kColor2 = nil;
static NSColor* kColor3 = nil;
static NSColor* kColor4 = nil;
[...]
static NSColor* kColorN = nil;
@implementation MyAppplicationDelegate
- (id)init {
[...]
kColor1 = [[NSColor colorWithDeviceRed: 0.6 green:0.6 blue:0.6 alpha:1.0] retain];
kColor2 = [[NSColor colorWithDeviceRed: 0.6 green:0.6 blue:0.4 alpha:1.0] retain];;
kColor3 = [[NSColor colorWithDeviceRed: 0.3 green:0.6 blue:0.6 alpha:1.0] retain];;
kColor4 = [[NSColor colorWithDeviceRed: 0.2 green:0.6 blue:0.8 alpha:1.0] retain];;
[...]
kColorN = [[NSColor colorWithDeviceRed: 0.5 green:0.6 blue:0.6 alpha:1.0] retain];;
[...]
Initializing your globals at this point guarantees that all the runtime machinery is already in place, and you also control the initialization order.
--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"It's extremely unlucky to be superstitious, for no other reason
than it is always unlucky to be colossally stupid." (Stephen Fry)
Weblog:
http://www.brockerhoff.net/bb/viewtopic.php
_______________________________________________
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.