discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: +initialize and static (class) variables


From: Richard Frith-Macdonald
Subject: Re: +initialize and static (class) variables
Date: Tue, 4 Sep 2001 16:31:42 +0000

On Tuesday, September 4, 2001, at 03:55 PM, Aurelien wrote:

Hi again,

(My previous problem with exceptions actually boiled down to this one)

I have an implementation like so:

static NSArray *advRequiredConstructors;
static AdvertisementFactory *advFactory;

@implementation AdvertisementFactory

+ initialize
{
  NSLog ( @"Calling initialize" );
  advFactory = [[AdvertisementFactory alloc] init];
  advRequiredConstructors = [[NSArray alloc] initWithObjects: nil];
}
....

As you can see, this is a singleton class. It turns out, strangely, that initializing advFactory works, but the program hangs when it's advRequiredConstructors' turn (i.e. commenting out the last line gets the job done)

Well, the above code looks fine, and works for me.
The only unusual thing about 'advRequiredConstructors = [[NSArray alloc] initWithObjects: nil]' is that it is a varargs method ... so it does begin to sound as if you have compiler problems, or perhaps you picked up an incorrect stdarg.h/varargs.h when you built the libraries?

I have to mention that I *did* something wrong from the start, but until now there was no problem with that. I still don't have my mind set about the AutoreleasePool bits, so I didn't create one in the beginning. Could that be the problem ? Also, it would be nice from one of you to send me the snippet that sets up the AutoreleasePool !

Since you aren't using autorelease, I don't think that the lack of a pool can be a problem (in any case, if you don't have a pool set up you should just leak memory and get warning messages logged - nothing
to cause a hang or crash).

However, FYI the simplest way to do a pool is -

{
  CREATE_AUTORELEASE_POOL(pool);

  // Your code here ...

  RELEASE(pool);
}

which is just a macro version (allowing for future use of boehm GC) of

{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  // Your code here ...

  [pool release];
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]