Fixing a leak in an NSFormatter
Fixing a leak in an NSFormatter
- Subject: Fixing a leak in an NSFormatter
- From: Darrin Cardani <email@hidden>
- Date: Wed, 7 Apr 2004 14:46:34 -0500
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:
- (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 ];
return retVal;
}
The problem is the allocation of the attributed string. As you can
see, I release the copy of the attributes and I autorelease the
original non-attributed string. It appears that when this method gets
called, though, that the caller is not releasing the old copy of the
attributed string. Am I supposed to be doing something different in
the way I allocate the attributed string so that it gets released? If
I autorelease it, I get a crash. So what am I doing wrong?
Thanks,
Darrin
--
Darrin Cardani - email@hidden
President, Buena Software, Inc.
<
http://www.buena.com/>
Video, Image and Audio Processing Development
_______________________________________________
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.