RE: Will this leak memory?
RE: Will this leak memory?
- Subject: RE: Will this leak memory?
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Fri, 5 Sep 2003 10:10:30 -0400
No, this pattern doesn't leak. defaultObject will stick around as long as
the class does, which is as long as your app runs. There's no way to
"unload" a class, so there's no occasion to release defaultObject. When
your app quits, the OS will reclaim the memory space occupied by
defaultObject.
dealloc gets called when an INSTANCE of your class is deallocated, and
dealloc should release only INSTANCE variables. If you had two instances
that were dealloced, and they each released defaultObject, your app would
crash (unless a new defaultObject had been created between releases).
Are you actually creating and destroying instances? If not, dealloc would
not be called.
Jonathan
>
-----Original Message-----
>
From: email@hidden
>
[mailto:email@hidden]On Behalf Of Neil Earnshaw
>
Sent: Friday, September 05, 2003 9:37 AM
>
To: email@hidden
>
Subject: Will this leak memory?
>
>
>
Will this implementation of the Singleton pattern leak memory in Obj-C?
>
>
+(id)defaultObject
>
{
>
static MyObject* defaultObject;
>
>
if ( !defaultObject ) {
>
defaultObject = [[MyObject alloc] init];
>
}
>
return defaultObject;
>
}
>
>
-(void)dealloc
>
{
>
NSLog(@"%@ dealloc",self);
>
...
>
[super dealloc];
>
}
>
>
The dealloc log message never gets called.
>
>
-Neil
>
>
Neil Earnshaw
>
Consultant Software Engineer
>
Object Software Engineers Ltd
>
email@hidden
>
Tel : 01747 854 852
>
Mbl : 07870 209 102
>
_______________________________________________
>
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.
_______________________________________________
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.