首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSCollectionView内存泄漏

NSCollectionView内存泄漏
EN

Stack Overflow用户
提问于 2020-12-25 05:49:21
回答 1查看 100关注 0票数 0

我有一个NSCollectionView,指定为我的DataSource和代表。

我有两个问题:

  1. 没有执行registerClass方法,而是尝试使用(非零) protoNib的3行注释代码来向NSCollectionView注册,这导致theItem总是为0.

使用类注册表选项的

  1. ,所有这些都可以正常工作。但是,如果删除willDisplayItemdidEndDisplayingItem存根,系统在第一次调用itemForRepresentedObjectAtIndexPath时就会占用内存(对这两个存根有数千次内部调用),并最终崩溃。仪器显示AppKit.

正在创建数千个4k @autoreleasepool content items

知道为什么会发生这种事吗?

代码语言:javascript
复制
-(void)awakeFromNib {
    [self registerClass:[MECollectionViewItem class] forItemWithIdentifier:@"EntityItem"];
//  NSString *nibName = NSStringFromClass([MECollectionViewItem class]);
//  NSNib *protoNib = [[NSNib alloc] initWithNibNamed:nibName bundle:nil];
//  [self registerNib:protoNib forItemWithIdentifier:@"EntityItem"];
    
    __weak typeof(self) weakSelf = self;
    [self setDelegate:weakSelf];
    [self setDataSource:weakSelf];
    ...
}

- (MECollectionViewItem *)collectionView:(NSCollectionView *)collectionView
     itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath;
{
        MECollectionViewItem *theItem = [self makeItemWithIdentifier:@"EntityItem"
                                                        forIndexPath:indexPath];
        return theItem;
}

-(void)collectionView:(NSCollectionView *)collectionView
      willDisplayItem:(NSCollectionViewItem *)item
forRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath
{
}

-(void)collectionView:(NSCollectionView *)collectionView
 didEndDisplayingItem:(nonnull NSCollectionViewItem *)item
forRepresentedObjectAtIndexPath:(nonnull NSIndexPath *)indexPath
{
}
EN

回答 1

Stack Overflow用户

发布于 2020-12-26 09:56:52

Appkit类不是设计成它们自己的委托的。NSCollectionView实现了几个NSCollectionViewDelegate方法并调用了委托。我不知道为什么会这样实现,感觉也不对,但事实就是如此。如果集合视图是它自己的委托,并且没有在子类中实现委托方法,那么调用将导致无限循环。解决方案:不要将delegate设置为self

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65445630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档