discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bogus retain via NSEnumerator


From: Marcel Weiher
Subject: Re: bogus retain via NSEnumerator
Date: Mon, 3 May 2004 22:54:18 +0100

[methods return autoreleased objects]

Danger, Will Robinson!

 This is a rough guideline for use, but not quite correct.

 Actually, the only guarantee you get is that you're not responsibe for
releasing it if there's no "alloc", "copy", "retain", "load" or "new" in
the name (load is a special case, but worth mentioning if you're
manually loading NIBs).

 However, they're not necessarily autoreleased. They may be singletons,
or they may be owned by the object you got them from.

Take as an example objectAtIndex:. If you retrieve an object using this
call from an array, you get a pointer to the object. You're not
guaranteed that it'll be autoreleased. If you delete the array after
calling objectAtIndex:, chances are your pointer to the object will be
invalid as well, unless you have retained the object explicitly to
prevent this.

 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.

Marcel


--
Marcel Weiher                           Metaobject Software Technologies
address@hidden          www.metaobject.com
Metaprogramming for the Graphic Arts.   HOM, IDEAs, MetaAd etc.
                1d480c25f397c4786386135f8e8938e4





reply via email to

[Prev in Thread] Current Thread [Next in Thread]