• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: static objects "autoreleased with no pool in place - just leaking"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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:15:05 -0800

Hello...

Well, purists would generally frown on this too, but only because you instantiate the objects in the init method. The problem is that you will leak objects if the class is ever instantiated more than once (which generally might not be the case for a class you use specifically as the application delegate, but it's still better to avoid the possibility).

The correct way to do this within the implementation of a class would be to use +initialize, which is called once for each class, the first time the class is used.


-- file MyAppplicationDelegate.m --


static NSColor* kColor1 = nil;
static NSColor* kColor2 = nil;
static NSColor* kColor3 = nil;
static NSColor* kColor4 = nil;
[...]
static NSColor* kColorN = nil;


@implementation MyAppplicationDelegate

+ (void)initialize
{
if (self == [MyAppplicationDelegate class])
{
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];
}
}

/* ... rest of MyAppplicationDelegate methods ... */

- - - - - - -


The (self == [MyAppplicationDelegate class]) check is required since you only want initialize to run once specifically for the MyAppplicationDelegate class when it is initialized, not when any potential subclasses are initialized...


Hope that helps,

Louis



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>
_______________________________________________
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.

  • Follow-Ups:
    • Re: static objects "autoreleased with no pool in place - just leaking"
      • From: Rainer Brockerhoff <email@hidden>
References: 
 >Re: static objects "autoreleased with no pool in place - just leaking" (From: Rainer Brockerhoff <email@hidden>)

  • Prev by Date: RE: At my wits end with NSSound!
  • Next by Date: Re: static objects "autoreleased with no pool in place - just leaking"
  • Previous by thread: Re: static objects "autoreleased with no pool in place - just leaking"
  • Next by thread: Re: static objects "autoreleased with no pool in place - just leaking"
  • Index(es):
    • Date
    • Thread