简体   繁体   English

reloadData UICollectionView上的内存泄漏

[英]Memory leak on reloadData UICollectionView

I'm working on my first project with iOS and should be considered a beginner with both Objective-C and xcode. 我正在使用iOS开发我的第一个项目,应该被视为使用Objective-C和xcode的初学者。

I'm building an app which displays sets of items in which the amount can vary from 1-200 items. 我正在构建一个显示项目集的应用程序,其中项目的数量可能在1-200个项目之间。 I'm retrieving the items from RSS feeds and parse the XML. 我正在从RSS提要中检索项目并解析XML。 I've been able to show the items in a UITableView without problems and it's working the way I want it to. 我已经能够毫无问题地在UITableView中显示项目,并且它以我想要的方式工作。

The app is for both the iPhone and iPad (testing on 6.1) and my goal is to show the items in a UICollectionView on the iPad and in a UITableView on iPhone. 该应用程序同时适用于iPhone和iPad(在6.1上进行测试),我的目标是在iPad上的UICollectionView和iPhone上的UITableView中显示项目。 I've been able to show the items in the collectionview but when I call the reloadData method the app crashes with the following error: 我已经能够在collectionview中显示项目,但是当我调用reloadData方法时,应用程序崩溃并出现以下错误:

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

In my search for the answer I read that turning on zombies could help me locate the problem, it's giving me this error: 在寻找答案的过程中,我读到打开僵尸可以帮助我找到问题所在,这给了我这个错误:

[CollectionCell release]: message sent to deallocated instance 0xc177470 [CollectionCell版本]:消息发送到已释放实例0xc177470

# Address Category Event Type RefCt Timestamp Size Responsible Library Responsible Caller 1507 0x16941940 CollectionCell Release 1 00:21.272.192 0 UIKit -[UICollectionView reloadData] 1508 0x16941940 CollectionCell Release 0 00:21.275.833 0 Foundation -[NSAutoreleasePool drain] 1509 0x16941940 CollectionCell Zombie -1 00:21.279.332 0 Foundation -[NSAutoreleasePool drain] #地址类别事件类型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 is my custom UICollectionViewCell class. CollectionCell是我的自定义UICollectionViewCell类。

I'll provide both the code for the UITableView and UICollectionView which I think is most relevant to the source of the problem: 我将同时提供我认为与问题根源最相关的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: 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) NewsCell(UITableViewCell)

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

CollectionCell (UICollectionViewCell) 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;
}

I hope I've included what's necesarry to solve this problem, I'm open to any advice to improve my code. 我希望我已经包括解决此问题的必要条件,我愿意接受任何改进我的代码的建议。

Seeing your code, and your comment that ARC is off, the memory leak is most likely happening because you're allocating space for your objects, but never releasing it. 看到您的代码,并指出ARC已关闭,则很可能发生内存泄漏,因为您正在为对象分配空间,但从不释放它。

If you're fairly new to programming, specifically dynamic memory management, I would suggest turning ARC on so you don't have to worry about releasing objects. 如果您刚开始编程,特别是动态内存管理,我建议您打开ARC,这样您就不必担心释放对象。

Also, follow your tutorial, if it has ARC on, turn it on, if it's off, turn it off. 另外,请按照您的教程进行操作,如果已启用ARC,请打开它,如果已关闭,则将其关闭。 This is because if it's on in the tutorial, it won't implement dealloc methods, which server the purpose of freeing up memory. 这是因为如果在本教程中启用了该功能,则不会实现dealloc方法,该方法旨在释放内存。

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

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