cocoa - 纠正 NSView 内存泄漏

标签 cocoa memory-leaks drag-and-drop nsview

我需要一些帮助来了解导致我的应用程序泄漏的原因。应用程序使用 ARC。我在窗口上有一个 NSView,用作文件的放置区。当您将文件拖到窗口上时,我会选择路径和文件名以在应用程序的其他方面使用。

当我通过 Instruments 运行应用程序时,一旦启动应用程序就会出现内存泄漏。以下是仪器返回内容快照的链接:

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

下面是我的自定义类 (dropZone) 的拖放代码。

我需要这方面的教训。我不明白如何读取 Instruments 返回的数据来进行更正。

感谢您的帮助。

@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

最佳答案

通常,当您使用 Instruments 时,您必须了解启动时系统中经常会出现一次性泄漏。

您从不关心 438 字节等一次性泄漏。您只关心连续的泄漏。

要找到这些内容,您可以像平常一样在 Instruments 下启动您的应用程序,但是一旦一切稳定,您就可以在 Instruments 中对其进行快照(例如,进行一次完整的拖放操作,然后进行快照) )。

然后你在快照后再拖放一些,然后看看是否有泄漏。这些是唯一重要的。 (除非你在发布时以某种方式泄漏了 500MB,但事实并非如此。)

enter image description here

关于cocoa - 纠正 NSView 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21392744/

相关文章:

iphone - 在 Objective-C 中动态实例化类,可能吗?

android - 为什么我创建的位图总是泄漏内存?

python - 将 XML 文件解析到 Google App Engine 数据存储区时的内存使用情况

c++ - 了解 Valgrind 的输出

iphone - iOS 从表格 View 单元格拖放到 ImageView

jquery - 确定浏览器是否具有拖放功能?

javascript - 如何使 ExtJS 拖放树只被复制——而不是从显示中删除项目?

iphone - 将对象从一个 NSDictionary 复制到另一个 NSDictionary

xcode - 列出 iCloud 中可用的文件

objective-c - Cocoa Touch - 向 Xbox 360 好友发送消息