Re: Fixing a leak in an NSFormatter
Re: Fixing a leak in an NSFormatter
- Subject: Re: Fixing a leak in an NSFormatter
- From: Stéphane Sudre <email@hidden>
- Date: Wed, 7 Apr 2004 22:26:00 +0200
On mercredi, avril 7, 2004, at 09:46 PM, Darrin Cardani wrote:
I have a custom subclass of NSFormatter. Using ObjectAlloc, I've found
that the formatter's
-attributedStringForObjectValue:withDefaultAttributes: method is
leaking memory. Here's what it does:
It's a bit surprising it's not crashing a bit later
- (NSAttributedString*)attributedStringForObjectValue:(id)anObject
withDefaultAttributes:(NSDictionary*)attributes
{
// Get the non-formatted string
NSString* regularString = [ self stringForObjectValue:anObject ];
NSAttributedString* retVal = NULL;
// Copy the passed in attributes
NSMutableDictionary* newAttribs = [ attributes mutableCopy ];
// Add our font to the attributes
[ newAttribs setObject:mFont forKey:NSFontAttributeName ];
// 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.
The ending code should be AFAIK:
[ newAttribs release ];
return [retVal autorelease];
}
_______________________________________________
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.