I know that I am suppose to use:
ObjectClass *tmpObject = [[ObjectClass alloc] init];
realObject = tmpObject;
[tmpObject release]
to initialise realObject
(where realObject
is an object within a class)
But now with ARC mode, releasing is automatic, do i still need to use this technique?
Can I simply use realObject = [[ObjectClass alloc] init];
?
If not is there any specific reason why it would leak?
Thanks
If you are compiling with -fobjc-arc (ie, using ARC) then not only do you not need to call
release
, it is a compiler error if you do so. When using ARC, it is the job of the compiler to insertretain
andrelease
calls for you.As Spencer said, if you compile with ARC enabled, you cannot call
release
at all. It is an error to do so and the compiler takes care of it for you.However:
The
tmpObject
in that case is entirely pointless for both ARC and manual retain-release. And, in fact, in manual retain-release, the above code will immediately release the object allocated, causing it to be deallocated (unlessObjectClass
internally does something odd) andrealObject
will be left with a dangling pointer.I.e. that code, as written, will cause a crash the first time anyone tries to message
realObject
.To clarify:
For ARC, you just do this: