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
As Spencer said, if you compile with ARC enabled, you cannot call
releaseat all. It is an error to do so and the compiler takes care of it for you.However:
The
tmpObjectin 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 (unlessObjectClassinternally does something odd) andrealObjectwill 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: