I have an ARC class with the following code:
[object doStuffWithObject:otherObject];
object
's -doStuffWithObject:
method is compiled with ARC, and is this:
- (void)doStuffWithObject:(id)otherObject
{
DoStuffHelper(object, otherObject);
}
DoStuffHelper
, a C function, is not compiled with ARC (for performance reasons). In DoStuffHelper
, do I need to call -retain
at the start for object
and otherObject
and -release
for them at the end?
See Avoid Causing Deallocation of Objects You’re Using in Advance Memory Management: Practical Memory Management, in which they point out that "received objects should typically remain valid throughout the scope of the calling method", i.e.
retain
andrelease
are not necessary, except with the following caveat:I'd be surprised if your helper function fell into one of those categories. You'll probably be fine without the
retain
andrelease
, but I only mention it for the sake of full disclosure.Obviously, you might need your own
retain
andrelease
if your functions need it for other reasons (e.g. if task is asynchronous or if you are otherwise doing something unusual where the object must outlive the scope of the calling method), but I suspect you would have mentioned it if you were doing one of those things.Also, if your utility function was creating and returning an object, then that would be a different matter (e.g. you'd generally
autorelease
objects that you were returning).