Re: Debugging retainCount
Re: Debugging retainCount
- Subject: Re: Debugging retainCount
- From: The Amazing Llama <email@hidden>
- Date: Wed, 16 Jul 2003 08:20:01 -0700
First off, Greg I love you. Yes, I was dumb and retaining/releasing
more often than I needed to.
To answer your second question, It crashes when the run loop ends and
the AutoreleasePool does it's magic. Then the last currentTick gets
another release and boom.
I'm very confused. OOM notes the crash, but pairs up the
retains/releases before it just fine.
On Wednesday, July 16, 2003, at 01:01 AM, Greg Titus wrote:
On Tuesday, July 15, 2003, at 11:09 PM, The Amazing Llama wrote:
Alright, if it's so easy, why does this code fail? I've been staring
at it for a day now.
- (void)drawRect:(NSRect)rect {
NSCalendarDate* currentTick = [[self startDate] copy];
NSCalendarDate* nextTick;
while ([currentTick isLessThanOrEqualTo:[self endDate]]) {
NSRectFill(NSMakeRect([self xForDate:currentTick]-1, 0, 2, 10));
nextTick = [currentTick
dateByAddingYears:10 months:0
days:0 hours:0 minutes:0 seconds:0];
[currentTick release];
currentTick = [nextTick retain];
}
[currentTick release];
}
Is there a reason for the extra retains/releases? The method would be
a lot simpler as:
- (void)drawRect:(NSRect)rect {
NSCalendarDate* currentTick = [self startDate];
while ([currentTick isLessThanOrEqualTo:[self endDate]]) {
NSRectFill(NSMakeRect([self xForDate:currentTick]-1, 0, 2, 10));
currentTick = [currentTick dateByAddingYears:10 months:0 days:0
hours:0 minutes:0 seconds:0];
}
}
That's the whole reason why autoreleased objects are convenient - not
having to explicitly do any memory management on them.
After this code, currentTick gets autoreleased. The currentTick after
this code is the last nextTick instantiated. I never autorelease it;
this autorelease comes from the dateByAddingYears... method itself,
which also allocs. Thus, when I get it it has a retain of 1 and 1
pending autorelease. I then retain it once, when I set currentTick to
point to it, and that is balanced by the release at the end of the
method.
So I get it with a net zero, and then I do a retain and a release.
I'm following the rules, as far as I can tell. Why does it crash?
Other than the unnecessary overhead, I don't see you doing anything
wrong in that code. Where does it crash?
Hope this helps,
- Greg
Seth A. Roby The Amazing Llama < mail or AIM me at tallama at mac dot
com>
"Life is like an exploded clown. It's really funny until you figure out
what just happened."
_______________________________________________
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.