• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Debugging retainCount
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Debugging retainCount


  • Subject: Re: Debugging retainCount
  • From: Greg Titus <email@hidden>
  • Date: Wed, 16 Jul 2003 01:01:14 -0700

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
_______________________________________________
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.

  • Follow-Ups:
    • Re: Debugging retainCount
      • From: The Amazing Llama <email@hidden>
References: 
 >Re: Debugging retainCount (From: The Amazing Llama <email@hidden>)

  • Prev by Date: Re: (10.0.x) option-clicking in statusbar hides active app (update)...
  • Next by Date: Rép : Cocoa and PostScript
  • Previous by thread: Re: Debugging retainCount
  • Next by thread: Re: Debugging retainCount
  • Index(es):
    • Date
    • Thread