Calling [objectInstance autorelease]
adds an object to the current NSAutoreleasePool
. When that pool receives a drain
message, it sends a release
to all the objects in the pool. If any of those objects' retainCount reaches 0, they are deallocated at that point. The purpose of autorelease is to allow you to mark an object to be released "some time in the future". This is especially useful for things like methods that return a newly allocated object but want to release it so that the caller doesn't have to take ownership of the returned object. A method might look like this:
- (id)myMethod {
id myObj = [[SomeClass alloc] init];
...
return [myObj autorelease];
}
The caller of myMethod
would then retain
the return value if they wanted to take ownership of the returned value or ignore it if not. When the current NSAutoreleasePool
is drained, myObj
will get a release message. If no other objects own it (i.e. have sent it a retain
message), it will get deallocated.
All of this is explained in the Cocoa Memory Management Programming Guide. Even if you've already read it, it's always worth an other read.
So, to answer your questions:
First, you should release theBackendResponse
. You will leak memory if you do not. You don't need to know what accountDictionary
does with the string: if it needs to keep a reference it will have retained theBackendResponse
. You have an ownership of theBackendResponse
because you alloc
'd it, so you must relinquish that ownership (via release
or indirectly via autorelease
).
Second, you must retain or copy the argument to setAccountDictionary:
if you want to keep a reference to that object or value respectively. The standard setter method looks something like this (assuming you do not need atomic semantics):
-(void)setAccountDictionary:(NSDictionary*)newDict {
if(newDict != accountDictionary) {
[accountDictionary release]; // You must relinquish ownership of the previous value. Messaging nil is OK, so this line is fine even if accountDictionary == nil
accountDictionary = [newDict copy]; //Since newDict may be mutable, we make a copy so that accountDictionary isn't mutated behind our back.
}
}
You must also remember to release
accountDictionary in the dealloc method:
- (void)dealloc {
[accountDictionary release];
[super dealloc];
}
Since you appear to be using NSViewController
, I assume you're on Leopard (OS X 10.5) in which case, you should probably be using @property
and the @synthesize
d getter/setter if possible. To do this, add a
@property (copy,readwrite) NSDictionary * accountDictionary;
declaration to the class @interface
. And add a @synthesize accountDictionary;
directive in the @implementation
block for your controller class.