Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-16-2011, 10:56 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 33
X-RaY is on a distinguished road
Question NSDictionary-MutableDeepCopy circling around issues

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
X-RaY is offline   Reply With Quote
Old 10-16-2011, 03:56 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by X-RaY View Post
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

Sounds like the analizer is wrong in this case. Copy methods Are supposed to return an object with a retain count of 1. Thus, when you call a copy method, you own the result.

It looks to me like oneCopy is a temporary object, so you should release it when you're done with it. BTW, mutableDeepCopy is a copy method, so it should return an object with a retain count of 1. The autorelease at the end does not belong there.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-17-2011, 02:27 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 33
X-RaY is on a distinguished road
Default

Ok
Thanks for the quick reply, really helpful!
Seems I've to bother about 1 issue less

Yet, why does the analyzer say oneCopy is not owned by the caller at the time it gets released?
X-RaY is offline   Reply With Quote
Old 10-17-2011, 09:29 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by X-RaY View Post
Ok
Thanks for the quick reply, really helpful!
Seems I've to bother about 1 issue less

Yet, why does the analyzer say oneCopy is not owned by the caller at the time it gets released?
I think I have your answer.

This from the docs:

Code:
You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).
Thus, the method name mutableDeepCopy does not meet the pattern of a copy method. It should be named "mutableCopyDeep" to follow the naming convention for copy methods.

The analyzer is not recognizing the mutableDeepCopy method as a copy method, so it assumes that it will (and should) return auto-released objects.

With ARC, following the naming conventions becomes even more critical. I would strongly suggest refactoring this class to call the method mutableCopyDeep instead of mutableDeepCopy.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-23-2011, 02:04 PM   #5 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 33
X-RaY is on a distinguished road
Default

Thanks so much!
That was indeed the correct answer.

sorry for the rather late reply. was on holiday

For others finding this thread:
final method prototype is called '- (NSMutableDictionary*)mutableCopyDeep;'

usage:
Code:
NSMutableDictionary *tempDict = [mutableDictionary mutableCopyDeep];
self.allNames = tempDict;
[tempDict release];
X-RaY is offline   Reply With Quote
Reply

Bookmarks

Tags
error, leak, mutabledeepcopy, nsdictionary, release

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
15 members and 387 guests
13dario13, 7twenty7, buggen, eski, EvilElf, glenn_sayers, j.b.rajesh@gmail.com, LunarMoon, morterbaher, n00b, QuantumDoja, sacha1996, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:26 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0