Use of alloc in class (factory) methods
Use of alloc in class (factory) methods
- Subject: Use of alloc in class (factory) methods
- From: Greg Hurrell <email@hidden>
- Date: Tue, 7 Jan 2003 13:23:24 +1030
Hi all,
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!
Cheers :-)
Greg
_______________________________________________
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.