Re: Xcode3.2 build and analyze warning for NSString allocation
Re: Xcode3.2 build and analyze warning for NSString allocation
- Subject: Re: Xcode3.2 build and analyze warning for NSString allocation
- From: Roland King <email@hidden>
- Date: Sun, 27 Sep 2009 17:18:34 +0800
I can't really give you a much better analysis than the static
analyzer is giving you. If you read the Memory Management Guide for
Cocoa you will see that only methods starting new or alloc or
containing copy should return objects transferring a retain count (but
please actually read the whole thing, there's more information there).
You have alloc/inited an NSString which you're then returning. alloc/
init needs a balancing release which you haven't given it before the
method end.
When you change it to stringWithFormat: that object isnt retained, so
you don't have the problem.
What you probably want to do here is autorelease the return value
before returning it and if your caller wants to retain the object, it
needs to do that. Although frankly the change to use stringWithFormat:
really does that for you anyway.
So either pattern works.
NSString *retval = [ [ NSString alloc ] initWithSomeInitializer: .. ];
[ retval autorelease ]; // <--- balances the alloc/init
return retval;
or
NSString *retval = [ NSString stringWithSomeInitializer: ... ];
return retval; // <--- ok as stringWithSomeInitializer is not an
alloc/copy type method.
If your second version of the code, with stringWithFormat: works and
doesn't crash, your original version was probably leaking the NSString.
On 27-Sep-2009, at 5:03 PM, Nick Rogers wrote:
Hi,
When I alloc and init a NSString the following way, there is warning
that:
Potential leak of an object allocated on line 526 and stored in
sizeDisp.
1. Method returns an Objective-C object with a +1 retain count
(owning reference).
2. Object returned to caller as an owning reference (single retain
count transferred to caller).
3. Object allocated on line 526 and stored into 'sizeDisp' is
returned from a method whose name
('tableView:objectValueForTableColumn:row:') does not contain 'copy'
or otherwise starts with 'new' or 'alloc'. This violates the naming
convention rules given in the Memory Management Guide for Cocoa
(object leaked).
The code is as follows:
if ([[tableColumn identifier] isEqual:@"Size"] == YES)
{
NSString *sizeDisp;// line 526
NSNumber *size = [data objectForKey:@kDeviceSize];
unsigned long long sizeInBytes = [size unsignedLongLongValue];
if (sizeInBytes < 1000*1000)
{
sizeDisp = [[NSString alloc] initWithFormat:@"%.2f KB", ((float)
sizeInBytes/1000)];
}
else if (sizeInBytes < 1000*1000*1000)
{
sizeDisp = [[NSString alloc] initWithFormat:@"%.2f MB", ((float)
sizeInBytes/(1000*1000))];// warning number 1 (as listed above)
}
else
{
sizeDisp= [[NSString alloc] initWithFormat:@"%.2f GB", ((float)
sizeInBytes/(1000*1000*1000))];
}
return sizeDisp;// warning 2 & 3 (as listed above)
}
When I do sizeDisp = [NSString stringWithFormat:@"%.2f MB", ((float)
sizeInBytes/(1000*1000))];
The warning disappears in the next build and analyze.
Can I hope for a small analysis of the above from anybody?
Thanks,
Nick
_______________________________________________
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
_______________________________________________
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