Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 7, 2011, 10:38 AM   #1
l0uismustdie
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Some memory management questions

Hello, I am coming towards the end of my first iPhone app and am running the Leaks performance tool to gather information about any leaks I have in my code. However, I'm having some trouble deciphering some of the messages I am getting. For example I have a number of leaks (the majority actually) coming from NSCFNumber and pointing to lines that look like this:

Code:
[memory setObject:[NSNumber numberWithInt:word%256] forKey[NSString stringWithFormat:@"%i",address+2]];
The whole method looks like:
Code:
-(NSMutableDictionary*)putword:(NSMutableDictionary *)memory atAddress:(int )address withWord:(int )word
{
	[memory setObject:[NSNumber numberWithInt:word%256] forKey:[NSString stringWithFormat:@"%i",address]];
	word=word/256;
	[memory setObject:[NSNumber numberWithInt:word%256] forKey:[NSString stringWithFormat:@"%i",address+1]];
	word=word/256;
	[memory setObject:[NSNumber numberWithInt:word%256] forKey:[NSString stringWithFormat:@"%i",address+2]];
	word=word/256;
	[memory setObject:[NSNumber numberWithInt:word%256] forKey:[NSString stringWithFormat:@"%i",address+3]];

	return memory;
}
Not that all the lines are identical but they all seem to have that [NSNumber numberWithInt:] component. Is there something I'm not understanding about these types of initializations in terms of releasing something here?

Thanks in advance for any help.
l0uismustdie is offline   0 Reply With Quote
Old Aug 7, 2011, 01:10 PM   #2
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
There are no memory leaks in the code you show.

The leaks tool works by periodically scanning memory to find any blocks of memory for which there are no other references. This is the definition of a memory leak. Because of the way it works, the leaks tool cannot tell you where in code a memory leak occurs. What it tells you is where a leaked memory block was created.

The leaks tool has told you that these NSNumber instances have been leaked and it told you where they were created. The actual line of code that leaked those objects is somewhere else. The leaks tool helps you to find leaks, but you have to use your own understanding of the code and the lifetime of the objects to figure our where and why the leaks occur.

One essential concept is that Objective-C apps consist of chains of ownership. All applications have objects that own other objects forming a chain of ownership. There are a very small number of roots of these chains. If an object that owns other objects is leaked the leaks tool tells you about all of the leaked objects. If you fix the leak of the single object that owns the other objects you fix all the leaks.

Your code is a clear example of one object leaking resulting in a lot of leaked objects. Almost certainly the leaked object is your mutable dictionary. Leaking of that object also leaks all of its contents. So focus on the lifetime of the mutable dictionary. It's of course possible that it is the owner of the mutable dictionary itself that has leaked or something further up the chain of ownership.

Always focus on container objects among the objects reported by the leaks tool.
PhoneyDeveloper is offline   0 Reply With Quote
Old Aug 7, 2011, 02:04 PM   #3
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Great thank you very much...that was a really informative answer. I will check out the dictionary and this is likely the cause of a couple other leaks I saw. Thanks again!
l0uismustdie is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Memory management is confusing! nashyo iPhone/iPad Programming 1 Mar 2, 2013 07:33 PM
Is Linux better at memory management than XP? RedCroissant Windows, Linux & Others on the Mac 5 Feb 5, 2013 12:20 PM
Memory Management StuffMattSays Mac OS X 10.7 Lion 12 Jul 3, 2012 02:48 PM
Strange Memory Management scbrain iPad 1 Jun 25, 2012 01:55 PM
iPad: Browser with better memory management porcupine8 Jailbreaks and iOS Hacks 5 Jun 6, 2012 02:45 AM

Forum Jump

All times are GMT -5. The time now is 11:45 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC