Re: Use of alloc in class (factory) methods
Re: Use of alloc in class (factory) methods
- Subject: Re: Use of alloc in class (factory) methods
- From: Nathan Day <email@hidden>
- Date: Tue, 7 Jan 2003 17:47:19 +1030
Don't bother, this is a common practice of just letting them hang
around, Apple does this themselves. You just slow down the quitting of
you app if you release resource individually that are going to be
cleared up in a fraction of a second anyway, it not like you are
wasting memory. If you need to do some cleanup before your program
quits, closing files etc, then use the App will quit notification. Also
you still need to init you _theDictionary.
On Tuesday, January 7, 2003, at 01:23 PM, Greg Hurrell wrote:
I have some code in one of my class (factory) methods that does an
alloc. (Example code):
+ (void)factoryMethod
{
static NSDictionary *_theDictionary = nil;
if (_theDictionary != nil)
{
// not a very meaningful alloc, but it is sufficient for this
example:
_theDictionary = [NSMutableDictionary alloc];
}
return _theDictionary;
}
I declare it static because I want it to persist.... the question is,
if I alloc it here, where can I release it?
If this were not a class/method it would be an easy matter of putting
the alloc in the -init: method and the release in -dealloc:.
But in this case there is no class/factory equivalent of -dealloc:. I
could put the alloc in +initialize: and move the static statement
outside of the method to give it global scope, but then where would I
put the release statement?
I was hoping that NSObject would have some kind of counterpart to
+initialize: called "+uninitialize" or something similar that would be
called on the destruction of an object.
I know this is just a minor leak -- the resource will be freed up
anyway when the program exits -- but it bothers me. I like to have
balance between my alloc and release calls!
Nathan Day
http://homepage.mac.com/nathan_day/
_______________________________________________
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.