Re: Fixing a leak in an NSFormatter
Re: Fixing a leak in an NSFormatter
- Subject: Re: Fixing a leak in an NSFormatter
- From: Shawn Erickson <email@hidden>
- Date: Wed, 7 Apr 2004 15:16:26 -0700
On Apr 7, 2004, at 1:39 PM, Darrin Cardani wrote:
At 10:26 PM +0200 4/7/04, Stiphane Sudre wrote:
It's a bit surprising it's not crashing a bit later
How is it surprising? Are you implying that I'm double releasing
something?
- (NSAttributedString*)attributedStringForObjectValue:(id)anObject
withDefaultAttributes:(NSDictionary*)attributes
{
// Get the non-formatted string
NSString* regularString = [ self stringForObjectValue:anObject ];
...
// Create an attributed string from the non-formatted one,
//but add our attributes
retVal = [ [ NSAttributedString alloc ]
initWithString:regularString attributes:newAttribs ];
// Clean up
[ newAttribs release ];
[ regularString autorelease ];
Why do you want to release regularString? It's already autoreleased.
Why do you say it's already autoreleased? Doesn't that depend on how I
created it in -stringForObjectValue: ?
Well is stringForObjectValue: returning an auto-released object or not?
We cannot tell if you don't say what it does. In theory, unless it is a
private method, it really should be returning an auto-released object
if that object is allocated by the method call.
The ending code should be AFAIK:
[ newAttribs release ];
return [retVal autorelease];
When I do that, I get a crash when the next autorelease pool releases
its objects.
To me this implies you have a problem someplace else outside of this
method, something is sending a release to an object it should not be
releasing.
In this method you are allocating the attributed string that gets put
into retVal and eventually returned to that caller. If you want to
follow the Cocoa memory management pattern correctly your method
(unless used privately and you want to take short cuts) should return
an auto-released object since you are responsible for releasing the
memory that you allocated not the caller of the method.
-Shawn
_______________________________________________
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.