|
From: | Richard Frith-Macdonald |
Subject: | Re: bogus retain via NSEnumerator |
Date: | Tue, 4 May 2004 06:31:54 +0100 |
On 3 May 2004, at 22:54, Marcel Weiher wrote:
Many objects *do* return autoreleased objects, however, and when writing your own code, it is a good idea to do that yourself, because it's one of the things that helps your app be thread-safe, and prevent other bugs.Danger, Will Robinson! :-)Autorelease will *not* help make your app thread-safe. At all. Only proper locking will.I also disagree that it prevents other bugs. It may *hide* them, but that only makes them more difficult to find in the long run.
Of course you need proper locking, and autorelease can hide (delay expression of) bugs,
but probably what was meant was writing code like - - (id) myAccessor { id result; [myLock lock]; // protects access to the 'myobject' ivar result = [[myObject retain] autorelease]; [mylock unlock]; return result; } rather than - - (id) myAccessor { return myObject; }The former is much slower ... but is thread safe and likely to prevent any problems which might be caused by the latter implementation when the caller expects to be able to use the returned value safely, but then calls a method (or another thread calls a method) which causes the ivar 'myObject' to be released.
Autorelease is a powerful tool ... but it does need to be used sparingly and with care.
[Prev in Thread] | Current Thread | [Next in Thread] |