objective-c - NSImage 泄漏?

标签 objective-c exc-bad-access nsimage nsoutlineview

所以我试图在我的应用程序中复制 SourceView 示例。我可以完美地生成源列表,很好。我还可以扩展容器,一切都显示正常。但是,当我选择列表中的项目时,应用程序崩溃并显示 EXC_BAD_ACCESS。

回溯

#0  0x7fff86532340 in objc_msgSend_vtable14
#1  0x7fff8508f622 in -[NSImage _deallocAuxiliaryStorage]
#2  0x7fff8508f5a1 in -[NSImage dealloc]
*#3 0x100002913 in -[SourceListCell setImage:] at SourceListCell.m:23
*#4 0x100001c31 in -[ProjectController outlineView:willDisplayCell:forTableColumn:item:] at ProjectController.m:166
#5  0x7fff85103085 in -[NSTableView preparedCellAtColumn:row:]
#6  0x7fff8511bc3f in -[NSTableView _drawContentsAtRow:column:withCellFrame:]
#7  0x7fff8511bbb5 in -[NSOutlineView _drawContentsAtRow:column:withCellFrame:]
#8  0x7fff8511acd8 in -[NSTableView drawRow:clipRect:]
#9  0x7fff8511a5cb in -[NSTableView drawRowIndexes:clipRect:]
#10 0x7fff8511a44c in -[NSOutlineView drawRowIndexes:clipRect:]

源列表单元格

@interface SourceListCell : NSTextFieldCell {
    NSImage *image;
}

@property(nonatomic, retain) NSImage *image;

@end

- (id)init {
    self = [super init];

    [self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];

    return self;
}

- (void)dealloc {
    [image release];
    image = nil;
    [super dealloc];
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    if (image != nil) {
        [image setSize:NSMakeSize(kIconImageSize, kIconImageSize)];
        // the cell has an image: draw the normal item cell
        NSSize imageSize;
        NSRect imageFrame;

        imageSize = [image size];
        NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + imageSize.width, NSMinXEdge);

        imageFrame.origin.x += kImageOriginXOffset;
        imageFrame.origin.y -= kImageOriginYOffset;
        imageFrame.size = imageSize;

        if ([controlView isFlipped])
            imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
        else
            imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
        [image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];

        NSRect newFrame = cellFrame;
        newFrame.origin.x += kTextOriginXOffset;
        newFrame.origin.y += kTextOriginYOffset;
        newFrame.size.height -= kTextHeightAdjust;
        [super drawWithFrame:newFrame inView:controlView];
    }
}

大纲 View 委托(delegate)

- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn*)tableColumn item:(id)item {
    if ([[tableColumn identifier] isEqualToString:COLUMNID_NAME]) {
            if ([cell isKindOfClass:[SourceListCell class]]) {
                //item = [item representedObject];
                NSImage *iconImage;
                iconImage = [[NSWorkspace sharedWorkspace] iconForFile:[item path]];
                [item setNodeIcon:iconImage];

                [(SourceListCell*)cell setImage:[item nodeIcon]];

            }
    }

}

最佳答案

问题出在我的自定义 NSTextFieldCell 中:我需要实现 copyWithZone。

关于objective-c - NSImage 泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1738255/

相关文章:

objective-c - 从 Storyboard中提取 nibs/xibs (iOS)

c - strcmp 说它有 EXC_BAD_ACCESS

swift - EXC_BAD_ACCESS Swift 1.2 中来自textureFromNode 的SKTexture 错误

objective-c - Cocoa,objective-c 如何调整 png 图像的大小?

Cocoa:将纹理图集值传递给 NSView

macos - 我的带图标的 NSButton 没有图标

ios - PKPaymentAuthorizationViewController 在 iOS 8.4 上意外崩溃

objective-c - 处理推送到不同 View 的通知

ios - 存储单例数组以供应用程序重新启动后使用?

ios - Apple 由于崩溃而拒绝了我的应用程序,无法重现