0

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
Paul
  • 189
  • 10

1 Answers1

2

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

Wil Shipley
  • 9,343
  • 35
  • 59
  • Wil, very helpful. Stupid of me not to realize those rare one time leaks at launch (unless sizable) really don't matter. There is also another when I drop an NSOpenPanel. Same symptoms. I'm guessing it's nothing to worry about? I need to educate myself further. I'm frustrated that I really have no idea what I'm looking at in Instruments. I'll look around for a resource and of course check the support docs. – Paul Jan 28 '14 at 13:12
  • Took me years to get at all familiar with Instruments, and part of it was seeing the creators use it at WWDC. I still don’t feel like an expert, but it’s such a powerful tool, none-the-less. See if you can find the WWDC videos on it, they really help. Yes, ignore the OpenPanel tiny leaks. – Wil Shipley Jan 28 '14 at 19:33