Question

I need some help understanding what's causing a leak in my app. App uses ARC. I have an NSView on a window functioning as a drop zone for files. When you drag a file onto the window I pick up the path and the file name to use in other aspects of the application.

When I run the application through Instruments, there is a memory leak as soon as I launch the application. Here's a link to a snapshot of what is returned in Instruments:

http://f-video.s3.amazonaws.com/leak.jpg

Below is the Drag and Drop code for my custom class (dropZone).

I need a lesson on this. I don't understand how to read the data returned in Instruments to make the correction.

Thanks for the help.

@implementation dropZone


- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
    // Initialization code here.
    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];

}
return self;
}

- (void)drawRect:(NSRect)dirtyRect {
/*NSRect bounds = [self bounds];
[[NSColor grayColor] set];
[NSBezierPath fillRect:bounds];*/
}

- (NSDragOperation)draggingEntered:(id )sender {
NSPasteboard *pboard;
NSDragOperation sourceDragMask;

sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];

if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
    if (sourceDragMask & NSDragOperationLink) {
        return NSDragOperationLink;
    } else if (sourceDragMask & NSDragOperationCopy) {
        return NSDragOperationCopy;
    }
}
return NSDragOperationNone;
}

- (BOOL)performDragOperation:(id )sender
{
NSPasteboard *pboard = [sender draggingPasteboard];

if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
    NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
    // Perform operation using the list of files
    NSLog(@"Dragged files");
    int i;
    for (i = 0; i < [files count]; i++) {


        NSLog(@"%@",[files objectAtIndex:i]);




        NSString * new = [files objectAtIndex:i];
        [[NSApp delegate] updateText:new];
    }



}
return YES;
}


@end
Was it helpful?

Solution

Generally when you’re using Instruments you have to understand there will often be one-time leaks in the system when launching.

You never care about a one-time leak of, like, 438 bytes. You only care about leaks that are continuous.

To find those, you launch your app under Instruments as normal, but then you snapshot it in Instruments once you’ve gotten everything stable (like, do a full drag and drop once, THEN snapshot).

Then you drag and drop some more after the snapshot, and see if there are leaks THEN. Those are the only ones that matter. (Unless, like, you’re somehow leaking like 500MB at launch, which you are not.)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top