So here's the thing... I'm using the 'NSDictionary-MutableDeepCopy' extension-class for a while now.
But I noticed a memory management warning in the Static Clang Analyzer lately...
the normal implementation:
Code:
#import "NSDictionary-MutableDeepCopy.h"
@implementation NSDictionary(MutableDeepCopy)
- (NSMutableDictionary*)mutableDeepCopy{ //Original one
NSMutableDictionary *returnDict = [[NSMutableDictionary alloc] initWithCapacity:[self count]];
NSArray *keys = [self allKeys];
for(id key in keys){
id oneValue = [self valueForKey:key];
id oneCopy = nil;
if([oneValue respondsToSelector:@selector(mutableDeepCopy)])
oneCopy = [oneValue mutableDeepCopy];
else if([oneValue respondsToSelector:@selector(mutableCopy)])
oneCopy = [oneValue mutableCopy];
if(oneCopy == nil)
oneCopy = [oneValue copy];
[returnDict setValue:oneCopy forKey:key];
[oneCopy release];
}
return [returnDict autorelease];
}
@end
The issue is at [oneCopy release]:
"Incorrect decrement of the reference count of an object that is not owned at this point by the caller"
But when I remove that line, the following issues pop up:
- "Potential leak of an object allocated on line 39 and stored into 'oneCopy'"
- "Potential leak of an object allocated on line 42 and stored into 'oneCopy'"
Line 39 and 42:
39: oneCopy = [oneValue mutableCopy];
42: oneCopy = [oneValue copy];
So i guess neither is ok.
Putting in the [oneCopy release] results in decrementing an reference count of an object not owned by me.
But removing them gives me 2 potential leaks....
Ow, btw: I call the method like this:
self.names = [self.allNames mutableDeepCopy];
I'd highly appreciate it if anyone could enlighten me about this problem...
Regards,
Me