views:

95

answers:

3

I was trying to track a strage memory allocation bug so I overrode the retain and release methods of my class. I noticed that when assigning an instance of this class to a property of another, the retain count of the object increased, but my redefined retain was never called.

How can this be possible? Are (retain) properties retaining the object without calling retain?

A: 

I wouldn't be surprised if synthesized properties would modify the retain count without calling retain or release.

Thomas Müller
+2  A: 

In my machine, the overridden retain was called. (I'm using 10.6.4. I checked this both on GCC 4.2.1 and clang 1.5.) Could you post your code?

Internally, the synthesized setter for a retain property uses objc_setProperty, the source code of which is available here. As you see, eventually it calls [newObject retain] when the property uses retain.

Yuji
I'll try to post a simple example later, as the code where I noticed this behavior is part of a large app. I forgot to mention that this was on an iPhone app, using the 3.2 SDK.
Mariano Ruggiero
A: 

Is garbage collection turned on? I don't believe retain is called under GC.

kubi
No, GC isn't on.
Mariano Ruggiero