Reputation: 21003
Does the following line leak this object [[NSMutableDictionary alloc] init]
?
[myCollection setObject:[[NSMutableDictionary alloc] init] forKey:@"myDictionary"];
I assume it doesn't because the object is never assigned to a reference... but at the same time I question it because I did alloc the object...
Would the following work as an alternative?
[myCollection setObject:[NSMutableDictionary dictionary] forKey:@"myDictionary"];
Upvotes: 0
Views: 53
Reputation: 243146
The first will leak, the second will not.
Assigning an object reference into a variable does not affect ownership of the object at all. You invoked alloc
, which means that you are the owner, regardless of whether you capture the results of that call at all. If you don't, you've leaked.
The second option ([NSMutableDictionary dictionary]
) will not leak, since +dictionary
returns a non-owned (autoreleased) object.
So in a nutshell, your intuition is correct. Congratulations! Many people who ask memory management questions here usually get it wrong. :)
Upvotes: 2