I have autoreleased objects that I am assigning to synthesized (retain) properties on an object but it is marking them as leaked. Does leaks just sometimes have false positives or am I missing something?
+6
A:
a retain property will overrule an autorelease.
EDIT for clarity, I don't mean overrule. I mean... it will prevent an autorelease from forcing a dealloc. See comments for all the grimy details.
you should be manually releasing that property in your dealloc to let autorelease run its course
Jasconius
2010-10-08 16:53:55
Or just set the property to nil like this: `self.MyProperty = nil` that will always release the item
Richard J. Ross III
2010-10-08 16:57:36
Nitpicking, but there is no "overrule". The synthesized accessor for the retain property lays an *additional* ownership claim on the object, which then needs balancing by a corresponding release. (This may come from another call to the accessor, as @Richard suggests.) The autorelease, meanwhile, autoreleases as normal.
walkytalky
2010-10-08 17:18:28
I was attempting to illustrate in laymen's terms. I'm not sure how helpful the complete technical overview would have been, given the nature of the question.
Jasconius
2010-10-08 17:21:38
So if I have this@property (nonatomic, retain) NSMutableArray *bar;foo.bar = [NSMutableArray array];I then have to release foo? I thought the property handled that.
brian
2010-10-08 17:37:05
[NSMutableArray array] is shorthand for [[[NSMutableArray alloc] init] autorelease] -- which gives a retain count of 1, then the foo.bar assignment increases the retaincount to 2. autorelease will then reduce it back to 1, then foo.bar = nil to 0, garbage collect
Jasconius
2010-10-08 17:39:27
but I thought the dealloc method automatically set properties to nil?
brian
2010-10-08 17:41:09
I"ve never heard of that but that doesn't mean it isn't true. Regardless, it's considered a good practice to make your intentions as a programmer very clear, so that other people know what you meant when you wrote it. No matter what, explicitly releasing it in dealloc will likely fix your Instruments leak.
Jasconius
2010-10-08 17:43:21
I can't believe I've missed this for so long.
brian
2010-10-08 17:45:15