Re: Debugging retainCount
Re: Debugging retainCount
- Subject: Re: Debugging retainCount
- From: The Amazing Llama <email@hidden>
- Date: Tue, 15 Jul 2003 23:09:14 -0700
David said:
Seriously, how hard is it to understand "... only send release or
autorelease to an object to which you have explicitly sent one of the
following messages: alloc, copy or retain." In other words, if you did
not allocate, copy or retain the object; then, do not try to release
it. IMHO, most of these problems stem from people over thinking this
whole process or poor design.
And publiclook said:
"Within a given block, every use of -copy*, -alloc* and -retain should
be paired with the use of -release or -autorelease."
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];
}
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?
_______________________________________________
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.