Re: String memory leak
Re: String memory leak
- Subject: Re: String memory leak
- From: Ondra Cada <email@hidden>
- Date: Mon, 3 Apr 2006 08:51:46 +0200
Paul,
On 3.4.2006, at 2:26, Paul Forgey wrote:
I'm not certain why the accessor is retaining it and then adding
that count to the autorelease pool.
Because that's the proper accessor pattern. Plain accessors are
dangerous; they should be used only in cases the efficiency makes it
necessary, and should be explicitly documented as such -- a good
example are primitive containers.
If the caller wants an instance of the glass string beyond the
lifetime of the class instance owning it or it being changed, it
should retain it itself.
Wrong. With plain accessors you are advocating:
So why not:
- (NSString *)glass {
return glass;
}
the caller would have to retain *all the time*, since you never can
know where a change may occur:
NSString *currentGlass=[object glass];
// any non-trivial code here
NSLog(@"%@",currentGlass);
With plain accessors this code is prone to crash as soon as the "non-
trivial code" happens to change the object's glass. Since Obj-C is a
dynamic language, you can never know whether it does: it may not now,
it may in future -- e.g., loading a plug-in with a category or a
poseAsClass.
You may advocate that in such a case it is the new code
responsibility not to change the glass, but that would not make
sense: then *no* new code could *ever* use any setter.
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden