• 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: Will this leak memory?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Will this leak memory?


  • Subject: Re: Will this leak memory?
  • From: "Alastair J.Houghton" <email@hidden>
  • Date: Fri, 5 Sep 2003 15:01:56 +0100

On Friday, September 5, 2003, at 02:36 pm, Neil Earnshaw wrote:

Will this implementation of the Singleton pattern leak memory in Obj-C?

+(id)defaultObject
{
static MyObject* defaultObject;

You need to initialise defaultObject... that is, it should be

static MyObject *defaultObject = nil;

rather than just

static MyObject *defaultObject;


if ( !defaultObject ) {
defaultObject = [[MyObject alloc] init];
}
return defaultObject;
}

Otherwise it looks OK.

The only other thing to note is that +defaultObject isn't thread-safe (that is, you could end-up with more than one MyObject instance if you call +defaultObject from multiple threads without a preceding call to +defaultObject).

-(void)dealloc
{
NSLog(@"%@ dealloc",self);
...
[super dealloc];
}

The dealloc log message never gets called.

Why would it, unless you did [[MyObject defaultObject] release] at some point in your program?

Kind regards,

Alastair.
_______________________________________________
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: Will this leak memory?
      • From: "Clark S. Cox III" <email@hidden>
References: 
 >Will this leak memory? (From: Neil Earnshaw <email@hidden>)

  • Prev by Date: Re: Will this leak memory?
  • Next by Date: RE: Will this leak memory?
  • Previous by thread: Re: Will this leak memory?
  • Next by thread: Re: Will this leak memory?
  • Index(es):
    • Date
    • Thread