• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Accessor methods and (auto)release <Memory trail>
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessor methods and (auto)release <Memory trail>


  • Subject: Re: Accessor methods and (auto)release <Memory trail>
  • From: Raphaël Delcroix <email@hidden>
  • Date: Wed, 31 Jul 2002 11:13:56 +0200

Well, first, thank you very much for all answers.

But am I sure that the instance variable is not used elsewhere ?
Because in that case I have to autorelease !

I fear I don't understand this. The autorelease is needed since newValue might be equal to value. Whether value is used elsewhere (which would be generally true) or not has nothing to do with it.

perfect, then !


Then, the author proposes this code :

Well, I don't want to start a war, but it looks to me like a madness. Since...
We agree ;)

[...]
Well, this is an easy way how to earn some money if you happen to be paid by a source line ;)))
:)))

}

But I understand less the use of a swap variable. This code would be
more simple :

- (void) setTheory:(Theory *)newTheory {
if (theory == newTheory) return;

I would not do that for the reasons above. If you have reasons to believe that theory==newTheory would occur often though, it might bring some -- in my estimate *utterly* unimportant and invisible -- speedup.
see below : that is not the only aim of this line.

[theory release];
theory = [newtheory copy/retain];
}

Alas, not only simple, but also incorrect in the case theory==newTheory && [theory retainCount]==1. If so, the release would *dealloc* the object, and thus the subsequent retain/copy sent to the same object would crash (or, with a multithreaded app, do even worse things).

(Heck, in this week I am writing it here the third or fourth time. Sigh.)
Yeah, and it seems to become automatic :-)
This is not incorrect here since it justify the presence of the test 'if (theory == newTheory) return;' !


So, what to do ?

In normal application, there's no harm in the simplest

[value autorelease]; value=[newValue retain/copy];

If you have *very* strong reasons to believe that you need some optimization here (eg. since the setter happens to be called many many many times indeed), you might go

[newValue retain]; [value release]; value=newValue;

With copy you need an extra variable -- IMHO, this is very slightly more readable than the idea from Stepwise, though of course functionally equivalent:

id o=[newValue copy]; [value release]; value=o;

Moreover, if, in your specific code, it is highly probable that newValue==value very often, you might go

if (newValue==value) return; ... the above ...


Great !

N.B. the reason of this thread is the presence of warnings in the documentation about the excessive use of -autorelease, not a fool search of optimisation...
But if I need it, now I know...

R.D.
_______________________________________________
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.

References: 
 >Re: Accessor methods and (auto)release <Memory trail> (From: Ondra Cada <email@hidden>)

  • Prev by Date: apple help
  • Next by Date: -setNeedsDisplay/-display don't work with NSTableView column titles !
  • Previous by thread: Re: Accessor methods and (auto)release <Memory trail>
  • Next by thread: Re: Accessor methods and (auto)release <Memory trail>
  • Index(es):
    • Date
    • Thread