Re: 'Build and Analyze' with XCODE 3.2.2
Re: 'Build and Analyze' with XCODE 3.2.2
- Subject: Re: 'Build and Analyze' with XCODE 3.2.2
- From: Scott Cherf <email@hidden>
- Date: Sat, 24 Apr 2010 13:38:50 -0700
I believe the problem is that you aren't releasing offset or gregorian before you return them. I expect that gregorian retains offset (or copies it and retains the copy without releasing the original).
In general, if you aren't don't intend to retain an allocated object, autorelease it and let the receiver retain it like this:
> NSDateComponents *offset = [[[NSDateComponents alloc] init] autorelease];
and
> NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
On Apr 24, 2010, at 1:02 PM, John Love wrote:
> Here's a sample snippet of my code:
>
> - (NSDate *)offsetDate:(NSDate *)fromDate
> byYears:(int)addYears
> byMonths:(int)addMonths
> byDays:(int)addDays {
>
> NSDateComponents *offset = [[NSDateComponents alloc] init];
> [offset setYear:addYears];
> [offset setMonth:addMonths];
> [offset setDay:addDays];
>
> NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
> return [gregorian dateByAddingComponents:offset toDate:fromDate options:0];
>
> }
>
>
> Builds and runs just dandy .. but "Build and Analyze" coughs up:
>
> // method returns an object with a +1 retain count (owning reference)
> NSDateComponents *offset = [[NSDateComponents alloc] init];
>
> ... and at the end of the method:
>
> // object allocated and stored into 'offset' is no longer referenced after this point
> // and has a retain count of +1 (object leaked)
> }
>
> It appears that the analysis is saying I should retain offset immediately after it is set with *offset =:
>
> NSDate *result = [gregorian dateByAddingComponents:offset toDate:fromDate options:0];
>
> [offset release];
>
> return result;
>
> By the way, identical gotchas are picked up by B & A for gregorian.
>
> John Love
>
>
>
> John Love
> Touch the Future! Teach!
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden