views:

42

answers:

1

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
Or just set the property to nil like this: `self.MyProperty = nil` that will always release the item
Richard J. Ross III
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
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
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
[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
but I thought the dealloc method automatically set properties to nil?
brian
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
I can't believe I've missed this for so long.
brian