繁体   English   中英

reloadData UICollectionView上的内存泄漏

[英]Memory leak on reloadData UICollectionView

我正在使用iOS开发我的第一个项目,应该被视为使用Objective-C和xcode的初学者。

我正在构建一个显示项目集的应用程序,其中项目的数量可能在1-200个项目之间。 我正在从RSS提要中检索项目并解析XML。 我已经能够毫无问题地在UITableView中显示项目,并且它以我想要的方式工作。

该应用程序同时适用于iPhone和iPad(在6.1上进行测试),我的目标是在iPad上的UICollectionView和iPhone上的UITableView中显示项目。 我已经能够在collectionview中显示项目,但是当我调用reloadData方法时,应用程序崩溃并出现以下错误:

“线程1:EXC_BAD_ACCESS(代码= 1,地址= 0,50c1ecd9)”

在寻找答案的过程中,我读到打开僵尸可以帮助我找到问题所在,这给了我这个错误:

[CollectionCell版本]:消息发送到已释放实例0xc177470

#地址类别事件类型RefCt时间戳大小负责的库负责的呼叫者1507 0x16941940 CollectionCell版本1 00:21.272.192 0 UIKit-[UICollectionView reloadData] 1508 0x16941940 CollectionCell版本0 00:21.275.833 0 Foundation-[NSAutoreleasePoolrain] 1509 0x16941940 CollectionCell Zombie -1 00:21.279.332 0基金会-[NSAutoreleasePoolrain]

CollectionCell是我的自定义UICollectionViewCell类。

我将同时提供我认为与问题根源最相关的UITableView和UICollectionView的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"NewsCell_iPhone";
UITableViewCell *cell = nil;
cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSArray *nib = nil;
    nib = [[NSBundle mainBundle] loadNibNamed:@"NewsCell_iPhone" owner:self options:nil];
    cell = [nib objectAtIndex:0];
} 

UICollectionView:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"CollectionCell";
UICollectionViewCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

if (cell == nil) {
    NSLog(@"Test");
    NSArray *nib = nil;
    nib = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell_iPad" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

NewsCell(UITableViewCell)

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
}
return self;
}

CollectionCell(UICollectionViewCell)

- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];

if (self) {
    // Initialization code
    NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" owner:self options:nil];

    if ([arrayOfViews count] < 1) {
        return nil;
    }

    if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
        return nil;
    }

    self = [arrayOfViews objectAtIndex:0];
}
return self;
}

我希望我已经包括解决此问题的必要条件,我愿意接受任何改进我的代码的建议。

看到您的代码,并指出ARC已关闭,则很可能发生内存泄漏,因为您正在为对象分配空间,但从不释放它。

如果您刚开始编程,特别是动态内存管理,我建议您打开ARC,这样您就不必担心释放对象。

另外,请按照您的教程进行操作,如果已启用ARC,请打开它,如果已关闭,则将其关闭。 这是因为如果在本教程中启用了该功能,则不会实现dealloc方法,该方法旨在释放内存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM