2012-01-09 19 views
1

更新: - [NSIndexPath行]:消息發送到釋放的實例0x895fe70一個Objective-C消息被在地址發送到解除分配的對象(殭屍):0xaa722d0

當我在設備運行我的應用程序和分析它說:

一個Objective-C消息被髮送到解除分配的對象(殭屍)在地址:0xaa722d0。

它顯示錯誤的:

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    int newRow = [indexPath row]; 
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row]: -1; 

我是很新的目標C編程和自兩天一直與下面的問題中掙扎,我最近創建使用的Xcode 3.2應用程序.6但在兩天前升級到Xcode 4,現在在我的下面的代碼中面臨內存釋放問題。我研究了使用儀器和啓用殭屍,並瞭解問題發生的位置,但無法解決它, 我請求一些指導..以下是我的代碼在Xcode 4.2中運行。

#pragma mark - 
#pragma mark Table Data Source Methods 
- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    return [list count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: 
          CheckMarkCellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CheckMarkCellIdentifier] autorelease]; 
    } 
    NSUInteger row = [indexPath row]; 
    NSUInteger oldRow = [lastIndexPath row]; 
    cell.textLabel.text = [list objectAtIndex:row]; 
    cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? 
    UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 

    return cell; 
} 

#pragma mark - 
#pragma mark Table Delegate Methods 
- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    int newRow = [indexPath row]; 
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row]: -1; 

    //----Captures the value selected in the list and shown in parent view. 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    SchneiderBMSAppDelegate *appDelegate = (SchneiderBMSAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    appDelegate.priorityValue = cell.textLabel.text; 

    NSLog(@"Product selected is: %@",appDelegate.priorityValue); 

    if (newRow != oldRow) 
    { 
     UITableViewCell *newCell = [tableView cellForRowAtIndexPath: 
            indexPath]; 
     newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: 
            lastIndexPath]; 
     oldCell.accessoryType = UITableViewCellAccessoryNone; 
     lastIndexPath = indexPath; 

    } 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

- (void)viewDidUnload { 
    self.list = nil; 
    self.lastIndexPath = nil; 
    [super viewDidUnload]; 
} 
- (void)dealloc { 
    [list release]; 
    [lastIndexPath release]; 
    [super dealloc]; 
} 
+0

請使用GDB中的BT命令更新錯誤描述的問題。 – Tirth 2012-01-09 03:50:42

+0

無論問題是什麼,只需添加一個[obj retain]調用就可以解決它。 – CodaFi 2012-01-09 03:51:01

+0

在Xcode中運行分析。什麼/問題在哪裏?在控制檯中什麼是錯誤信息? – zaph 2012-01-09 04:12:13

回答

3

你似乎認爲當lastIndexPath被釋放時,它將成爲零。事實並非如此。您或者需要保留該對象(最好使用setter而不是直接設置實例變量),或者不存儲對該對象的引用。

+0

查克,感謝您的回覆,生病立即嘗試.. – user1137762 2012-01-09 04:47:47

+0

查克,它的工作! :) 非常感謝!作爲建議我取代了這一行:lastIndexPath = indexPath;有了這個 - > lastIndexPath = [indexPath retain]; – user1137762 2012-01-09 05:23:15

0

我有一個應用程序使用了幾乎相同的代碼(無法回想我參考了哪個教程),我在iOS 4.3中完成但未發佈到App Store。當我最近重新啓動應用程序並嘗試在iOS 5中運行它時,發生了完全相同的問題。我可以通過更改該行來使其工作:

lastIndexPath = indexPath;

self.lastIndexPath = indexPath;

然後它在iOS 5中運行正常。我猜它通過保留對象來實現與查克推薦的相同的功能。但爲什麼iOS 5照顧,而不是iOS 4.3?這可能是由於iOS 5中的ARC嗎?

+0

我自己是一個新手,但我猜測iOS 5爲我們做了內存管理,而在iOS 4.3中,我們不得不照顧管理讓我把這段代碼放在4.3中的內存。只是我的兩分錢.. – user1137762 2012-03-06 20:40:36

相關問題