Say I have a class DoStuff, and that class has two methods, like so
- (NSMutableDictionary* returnToCaller) methodOne : (NSString* ) myString {
NSMutableDictionary* bundleOfJoy = [[NSMutableDictionary alloc] init];
if (myString) {
bundleOfJoy = [self methodTwo];
}
return bundleOfJoy;
}
- (NSMutableDictionary* returnToMethodOne) methodTwo {
NSMutableDictionary* anotherDictionary = [[NSMutableDictionary alloc] init];
[anotherDictionary setObject: @"hardcodedstring" forKey: @"theKey"];
return anotherDictionary;
}
ok, so bear with me as my memory-management-fu is kind of weak. I can’t release the two dictionaries manually created after the returns as the release command won’t be called. I can’t do it before the returns or I pass no values. My understanding then is the way to handle this is with an autorelease pool…
pool = [[NSAutoreleasePool alloc] init];
and init my objects as such
NSMutableDictionary* anotherDictionary = [[[NSMutableDictionary alloc] init] autorelease];
and then call
[pool drain];
so, if that is correct, where do I init pool? In awakeFromNib? And where do I call [pool drain]?
if this is incorrect could someone straighten me out (but please type slowly) : D
thanks
There is an automatic NSAutoreleasePool in each thread and you don’t have to create one instead you’re creating a new thread.
Use [pool release]; instead of [pool drain] unless to have a memory leak.
For your code it’s the responsibility of the method to release allocated object so add