I'd recommend against the approach you're describing... your instinct to ask a question about it on SO is correct as this probably going to lead to trouble.
Objects are responsible for managing their own internal state, and having a delegate interfere with that internal state in this way is quite unusual. The normal practice for cleaning up memory is to have to have MyObject
release memory in its dealloc
method:
- (void)dealloc {
[item release];
[super dealloc];
}
In fact, if you are implementing dealloc
as you should to tidy up MyObject
, having the delegate send a release message to the object at itemProperty
will mean that the itemProperty
objects gets released twice, which will leave itemProperty's retain count in an unexpected state and may cause it to be deallocated early. If you ever change the app so that other objects may be using itemProperty, they will find it's been mysteriously deallocated after the delegate is called.
On the other hand, if you don't tidy up memory in dealloc
, you run into a situation where you are relying on having the delegate being called to ensure that memory is properly managed. Can you really guarantee that the delegate method will ALWAYS be called at some point in your app? Without fail? Even if there are exceptions or other error conditions that may cause an unusual execution flow for your application?
Even if you can guarantee this, as the app logic changes, this may no longer be the case and you'll forget that the delegate is doing the memory management, leading to a leak or a segfault. Certainly, other cocoa developers maintaining your code would not think to look at the delegate for memory management and would probably have a hard time debugging issues that arise.
In this case, have the delegate just use the itemProperty
without retaining, releasing, or autoreleasing it (unless, of course, the delegate needs to guarantee that the itemProperty will still be around after, perhaps, MyObject is gone). When MyObject is deallocated, the itemProperty data will be released then.