簡體   English   中英

UITableView cellForRowAtIndexPath中的EXEC_BAD_ACCESS

[英]EXEC_BAD_ACCESS in UITableView cellForRowAtIndexPath

我的UITableView返回EXEC_BAD_ACCESS ,但為什么!

看到這段代碼片段!

加載UITableView工作正常,所以allXYZArray != nil並填充!

然后將tableview滾動到底部並備份導致它崩潰,因為它重新加載方法cellForRowAtIndexPath

它失敗了:

    "NSLog(@"allXYZArray::count: %i", [allXYZArray count]);"

        (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAt

IndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];


cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
@try
{ 
if (allXYZArray == nil) {
   NSLog(@"nil");
   allXYZArray = [ToolBox getMergedSortedDictionaries:allXYZGiven SecondDictionary:allXYZSought];
}
NSLog(@"%i", [indexPath row]);
NSLog(@"allXYZArray::count: %i", [allXYZArray count]);

EXC_BAD_ACCESS表示您的程序正在嘗試訪問無效或無法從您的進程訪問的內存地址。 當您嘗試向已經解除分配的對象發送消息時,最常發生這種情況。 因此,調試EXC_BAD_ACCESS的第一步是確定程序嘗試向崩潰發生時發送消息的對象。 通常答案並不明顯,在這種情況下, NSZombieEnabled是一個很好的工具,用於識別導致崩潰的代碼行。

在你的情況下,當你調用[allXYZArray count]時,你已經確定發生了崩潰,使得allXYZArray成為我們的主要嫌疑人。 這個對象是從+[ToolBox getMergedSortedDictionaries:SecondDictionary:] ,所以很可能你的bug是在該方法的實現中。 我猜它會返回一個已經被釋放的對象而不是自動釋放的對象,正如Cocoa內存管理編程指南所規定的那樣。 (順便說一句,這是SDK中最重要的文檔之一。我建議每月重讀一次,直到其政策和技術成為第二天性。)

好的,重用一個單元格,並不能保證單元格能夠正確初始化:

UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];

單元格有時會為空(特別是我第一次猜)。

檢查單元格是否為空,如果是,請正確初始化它。

if (cell == nil)
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM