Tracking down a memory leak
So I was running my application through the leak analyzer with Instruments. Surprisingly, only one leak showed up. It was at a line looking like this:
self.myDictionary = [[NSMutableDictionary alloc] init];
and some subsequent assignments into the dictionary. It took me a few hours of trying to figure out if I could believe this (how can I leak something here? I am using the property assignment, that should take care of releasing the old guy, right?)…
The property is defined as a (retain) property. And then it suddenly hit me.
In the set property, the old one will get released properly. Then the new one get’s assigned, and retained. So after the assignment the ref count is increased again, so self.myDictionary has a ref count of 2, instead of one.
Duh. Duh.
While this is obvious for most people, it really took me a while to just “see” it. The line looks so innocent, so “i am going to take care of you”.