2013-10-02 42 views
0

我正在使用iOS開發我的第一個項目,應該被視爲Objective-C和xcode的初學者。reloadData上的內存泄露UICollectionView

我正在構建一個應用程序,其中顯示的項目數量可以從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 0 x16941940 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]

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; 
} 

我希望我已經包括瞭解決這個問題的必要條件,我願意接受任何改善我的代碼的建議。

+0

我不認爲你已經包括了一切。看看這個錯誤,它說[CollectionCell release] < - 我的代碼中沒有發佈。 –

+0

我沒有在我的代碼中的任何地方發佈CollectionCell,至少不是手動的,因爲我找不到發佈的調用。我猜它與initWithFrame方法有關,我從教程中獲得了它。 – user2833970

+0

ARC打開還是關閉? –

回答

0

看到你的代碼,並且你的評論ARC關閉,內存泄漏很可能發生,因爲你爲對象分配空間,但從不釋放它。

如果您對編程相當陌生,特別是動態內存管理,我會建議打開ARC以便您不必擔心釋放對象。

此外,請按照您的教程,如果它已打開ARC,打開它,如果它關閉,則將其關閉。這是因爲如果它在教程中,它不會實現dealloc方法,它釋放內存的目的。

+1

感謝您和Nerkatel的建議。打開ARC導致一系列錯誤,因爲我已經(自動)釋放,因爲它包含在我遵循的其他教程中。我會在下一個項目中打開ARC。 Nerkatel的答案現在已經解決了這個問題,我會更多地閱讀內存管理,以防止我犯同樣的錯誤。 – user2833970