Question about Cocoa leaks
Question about Cocoa leaks
- Subject: Question about Cocoa leaks
- From: Laurent Daudelin <email@hidden>
- Date: Fri, 04 Mar 2011 17:54:12 -0800
I asked a similar question to this one in the xcode-users list because I thought it related to Instruments but, actually, the question maybe has more to do with Cocoa.
After reading the very interesting and informative blog from Bill Bumgarner (bbum) titled "When is a Leak not a Leak? Using Heapshot Analysis to Find Undesirable Memory Growth" and started testing an application that scans entire disks and gathers information about some of the items it scans.
With such large number of allocations, I want to check that there are no memory leaks. Right now, I'm stumped at a few Cocoa classes that seem to be leaking. Not that much but still, I'd like to get to the bottom of this. In Instruments, I started my application with the Allocations tool. I did mark the heap after the application was started, then did a scan, closed the results window, marked the heap again and repeated 6 times (I had to stop at that time because Instruments was becoming very sluggish and all the free memory on my machine was used).
In the last 5 heapshots, I notice a few Cocoa classes that seem to leak something.
For example, given that instruction:
pathToCompare = [destinationDirectoryPath stringByAppendingPathComponent:pathPartToAdd];
I get the following stack trace which leads to a calloc call and 400 bytes allocated:
0 libSystem.B.dylib calloc
1 libobjc.A.dylib _internal_class_createInstanceFromZone
2 libobjc.A.dylib _internal_class_createInstance
3 Foundation NSAllocateObject
--- 1 frame omitted ---
5 Foundation -[NSString(NSPathUtilities) stringByAppendingPathComponent:]
Or another occurrence with NSString:
result = [NSString stringWithUTF8String:(char*)buffer];
generates this stack trace:
0 CoreFoundation _CFRuntimeCreateInstance
--- 1 frame omitted ---
2 CoreFoundation CFStringCreateWithBytes
3 Foundation -[NSPlaceholderString initWithBytes:length:encoding:]
4 Foundation +[NSString stringWithUTF8String:]
Even though the "buffer" is freed. Here is the complete instructions set:
UInt8* buffer = malloc(4*PATH_MAX);
if (FSRefMakePath(theRef, buffer, 4 * PATH_MAX - 1) == noErr) {
result = [NSString stringWithUTF8String:(char*)buffer];
}
free(buffer);
So, can anybody explain what is going on here and whether I should be concerned by those leaks? For each heapshot, I scan 31770 items on disk and each heapshot results in between 8.84KB and 16.97KB leaked each time. The other strange observation is that there are exactly 14 instances of NSPathStore2 and 14 instances of CFString that are leaked in each heapshot even though the loop where the instructions above come from is iterated about 2,200 times. Basically, I scan the folder with 31,770 items in it and will retain 2,200 items of it that I process further.
Anyone has any idea? I would think (and certainly hope) that Cocoa classes are thoroughly debugged and are run through extensive tests as to not waste memory. But, I can't understand how an instruction like stringByAppendingPathComponent: or stringWithUTF8String: can result in memory leaked.
-Laurent.
--
Laurent Daudelin
AIM/iChat/Skype:LaurentDaudelin http://www.nemesys-soft.com/
Logiciels Nemesys Software email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden