Re: newbie memory question
Re: newbie memory question
- Subject: Re: newbie memory question
- From: Harilaos Skiadas <email@hidden>
- Date: Tue, 16 Nov 2004 09:30:19 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
Daniel,
>I removed the calls to alloc and init under the init
method and simply
I would probably set the variables to point to nil in
the init method. They get some unpredictable initial
value of their own IIRC, and if you call their value
before setting it, you have a good chance of
unpredictable behavior.
>ran the code as before. I still get an initially high
retain count the
>first time I use the variables whatTheUserTyped and
theLength. Do
>things hold onto to variables in the background?
>
>I have read the articles on memory you (and others)
have suggested and
>was confused by one thing. If you have a setter
method:
>
>(void) setVar: (NSFloat) f
>{
> [f retain]; // The assumption is f is
autoreleased, but would
>autorelease dump it before I use
//it here?
No, it won't, but it might when you leave the method,
so retain it to make sure it doesn't
> [var release]; // This releases memory for what
var previously
>pointed to?
Yes, exactly.
> [var = f]; // and now we set it to f.
>}
You might want to first check if var=f and avoid a
retain and a release in that case, like:
if (f!=var) {
[f retain];
[var release];
var=f;
}
Take note that if you are working with strings,
depending on the situation you might want to do things
a bit differently. If you care about the particular
value of the string at that moment, and you don't want
the value you are seing to change if someone else
changes the value of f, the you want [f copy] instead.
>
>then doesn't f keep getting retained more than it
should. (This was the
The general idea is that if you use an accessor for a
variable, then you own that variable. This means that
at some point in life you alloced/inited, copied or
retained the object it refers to. So you are
responsible for releasing it also. A whole bunch of
other objects might be owning it also, but you don't
care about that, if you release it when you are done
with it, your job is done.
>format suggested in the article.) And if you release
var, isn't there a
>danger that it's retain count drops to -1 (not
retained)?
It would drop to zero at worst I would think. you are
supposed to have retained var when you got it, so it
has retain count at least 1.
>In the code that I originally wrote, I tried
releasing my variables
>after updateUI, but then when I called the
countLetters again, the
>program crashed. If I did not want to use accessor
methods, is there a
>way to release things safely?
>
In your code as it stands now, you have not retained,
alloced/inited, copied anything, so you do not own
anything, and hence you are not responsible for
releasing them, so you should not.
The thing you want to ask yourself, for each variable
appearing, is: Do I want to own this variable or not?
If yes, you need to retain/release at a whole bunch of
points, and better to use an accessor for this.
If no, you don't have to worry about releasing
anything.
The question is, the class that does all this work
and containing the methods countLetters etc, does it
need to know the value of whatUserTyped outside the
countLetters method? As the code is now, the value of
whatUserTyped might become invalid the moment the
program leaves countLetters.
>Also, presumably because of the use of convenience
methods, the retain
>counts go to -1 and 1, respectively, after updateUI.
Does that still
>constitute a memory leak. Here is the revised code
(the init method now
>simply calls [super init] with no initialization of
variables?
>
>- (IBAction)countLetters:(id)sender
>{
> whatUserTyped = [theStringToCount stringValue];
> numLetters = [whatUserTyped length];
> theLength = [NSString stringWithFormat:
@"%i", numLetters];
> [self updateUI];
>}
>
>- (IBAction)startOver:(id)sender
>{
> whatUserTyped = @"";
> numLetters = 0;
> theLength = [NSString stringWithFormat: @"%i",
numLetters];
> [self updateUI];
>}
>
>- (void)updateUI
>{
> [theStringToCount setStringValue: whatUserTyped];
> [theAnswer setStringValue: theLength];
>}
>
Best,
Haris
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
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