Re: Debugging retainCount
Re: Debugging retainCount
- Subject: Re: Debugging retainCount
- From: Brent Gulanowski <email@hidden>
- Date: Wed, 16 Jul 2003 10:19:55 -0400
On Wednesday, July 16, 2003, at 02:09 AM, The Amazing Llama wrote:
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];
(retaincount+)
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];
(retaincount-)
currentTick = [nextTick retain];
(retaincount+)
}
[currentTick release];
(retaincount-)
}
After this code, currentTick gets autoreleased.
What do you mean it "gets" autoreleased? It was already sent an
autorelease message when you instantiated it with dateByAddingYears:...
Have you sent an additional autorelease message, or are you stating the
known fact that it gets released when the current autorelease pool gets
released?
The currentTick after this code is the last nextTick instantiated.
Both pointers point to the same object. You are, as suggested,
over-thinking. If you only made one pass through the loop, the retains
and releases would balance. The loop contains one retain and one
release, so successive passes through the loop do not affect the
balance. Any additional releases (including autoreleases) sent to the
object (there's only one left that you have a pointer to) are going to
cause a crash.
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.
Brent Gulanowski
--
Mac game development news and discussion
http://www.idevgames.com
_______________________________________________
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.