Re: Fixing a leak in an NSFormatter
Re: Fixing a leak in an NSFormatter
- Subject: Re: Fixing a leak in an NSFormatter
- From: Ondra Cada <email@hidden>
- Date: Wed, 7 Apr 2004 23:46:25 +0200
Darrin,
On Wednesday, Apr 7, 2004, at 21:46 Europe/Prague, Darrin Cardani wrote:
- (NSAttributedString*)attributedStringForObjectValue:(id)anObject
withDefaultAttributes:(NSDictionary*)attributes
{
// Get the non-formatted string
NSString* regularString = [ self stringForObjectValue:anObject ];
NSAttributedString* retVal = NULL;
To pick nits, for object values you should use "nil" instead. It's the
very same zero value of course, but it makes the code more intelligible.
NSMutableDictionary* newAttribs = [ attributes mutableCopy ];
Unless you have profiled the code and found it's a bottleneck (which I
doubt), you should use "[[attributes mutableCopy] autorelease]"
instead, and of course do not release below (*). The difference is that
your code (a) would leak if the following code happens to raise an
exception, (b) is error-prone since you may overlook the connexion and
add, say, return (or so) in between.
[ newAttribs setObject:mFont forKey:NSFontAttributeName ];
retVal = [ [ NSAttributedString alloc ]
initWithString:regularString attributes:newAttribs ];
The same applies here, you should autorelease immediately. The
alloc/init... combo is one of the very small number of ways how to get
an object which must be released (mutableCopy is another, copy a third
one, and it's about all save the somewhat obsoleted new and the
...withZone: variants).
That's also the reason the string leaks: you don't ever release it, so
it is bound to.
[ newAttribs release ]; (*)
[ regularString autorelease ];
This is a crasher though. Getting an object by another method than
those listed above (alloc/init, copy, new, and their variants), does
*NOT* mean you own it, so you *CANNOT* release it (unless you have
retained it before).
If your app does not crash, consider yourself lucky (or, rather,
suspect another leaker bug which happens to nullify the problem ;)))
return retVal;
}
The problem is the allocation of the attributed string. As you can
see, I release the copy of the attributes
All right...
and I autorelease the original non-attributed string.
Wrong...
It appears that when this method gets called, though, that the caller
is not releasing the old copy of the attributed string.
Sure, see above...
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.
Perhaps then the crasher bug would bite your back ;)
It is actually pretty probable, for NSAttributedString would copy the
string, for an immutable string copy means a retain, so the string of
yours is kept alive by the leaker attributed string. If you remove the
leak, the "regular" string goes too--since it was released once too
many.
---
Ondra Hada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
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.