Re: Memory management question
Re: Memory management question
- Subject: Re: Memory management question
- From: Alastair Houghton <email@hidden>
- Date: Wed, 24 Mar 2004 22:10:39 +0000
On 24 Mar 2004, at 21:24, Ondra Cada wrote:
>
Alastair,
>
>
On Wednesday, Mar 24, 2004, at 20:14 Europe/Prague, Alastair Houghton
>
wrote:
>
>
> something simple like:
>
>
>
> - (void)setStringValue:(NSString *)string
>
> {
>
> [string retain];
>
> [myString release];
>
> myString = string;
>
> }
>
>
>
> will work just fine...
>
>
... in most cases, but can fail too, presumed a plain getter of kind
>
>
-(NSString*)stringValue { return myString; }
>
>
is used. The problem may be risen by the fact someone can use the
>
getter, then use some code which happens to call the setter, and then
>
use the value from the getter:
>
>
NSString *s=[foo stringValue];
>
// some code here which, whatever indirectly, calls the setter
>
[s length]; // a crasher
Agreed and I'm well aware of the argument. Personally I follow the
approach advocated by Marco and Marcel, for instance in this thread
http://cocoa.mamasam.com/COCOADEV/2002/08/1/41613.php
since I find that I don't write code like the example you just gave,
and avoiding extraneous autoreleases is a performance win. Also as has
been pointed-out before, if you do accidentally write such code, you'll
normally cause a crash, which makes it pretty easy to debug.
BTW, autoreleasing in the setter isn't a solution to the problem,
because someone could wrap an autorelease pool around the call to the
-setStringValue: method, which would result in the same crash you talk
about with the simple methods.
Kind regards,
Alastair.
--
http://www.alastairs-place.net
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.