Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
Edinburgh, UK
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.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.