On 29/05/2009, at 11:19 AM, Erg Consultant wrote:

- (id)init
{
  if( !gReg )
   {
       if( ( self = [ super init ] ) )
       {
           // Zero out members...

           keys = nil;

           lastFlushTime = nil;
       }
   }

   return self;
}


This is a little broken.

-init must always be allowed to run, so testing for the existence of gReg is wrong. I know you're trying to create a kind of singleton, but this isn't the way to do it.

Also, initialising members to zero isn't necessary, as alloc does that.

So, removing the unnecessary and incorrect things here, you end up with:

return [super init];

Hmmm... looks like you can dispose of init altogether. I assume your object inherits from NSObject?


A good way to make a singleton that avoids a global is to use a class method like so:

+ (MyObject*)   singleton
{
    static MyObject* sSingle = nil;

    if( sSingle == nil )
        sSingle = [[self alloc] init];

    return sSingle;
}
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to