0

我正在調用Thread 1錯誤:程序收到信號:這裏「EXC_BAD_ACCESS」 是我的代碼EXC壞訪問iphone錯誤

[gameArray removeLastObject]; 
[gameArray addObject:shotArray]; 
[gamesArray removeObjectAtIndex:gameNumber]; 
[gamesArray insertObject:gameArray atIndex:gameNumber]; 
NSString *path = [self findGamesPath]; 
[NSKeyedArchiver archiveRootObject:gamesArray toFile:path]; // the error is here 

爲什麼會出現錯誤?是被釋放太多次了嗎?

這裏是findGamesPath代碼

- (的NSString *)findGamesPath { 的NSArray *路徑= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * documentFolder = [paths objectAtIndex:0]; NSString * result = [documentFolder stringByAppendingPathComponent:@「iShotTrackGames.plist」]; 返回結果; }

回答

1

更可能的是,某些東西被釋放了太多次。打開殭屍檢測並重試。

然而,在你這樣做之前,嘗試在代碼上執行「構建和分析」並修復它識別的任何問題。

我看到它被標記爲「線程安全」。爲什麼?即在上面的代碼運行時,其他線程可能會做些什麼?

0

EXC_BAD_ACCESS表示你在某處有一個錯誤的指針。這些錯誤通常很容易找到,因爲錯誤的指針是發生錯誤的行上調用的方法的參數之一。不過,在這種情況下,存檔器將會遍歷由gamesArray指向的整個對象圖,而壞指針可能真的存在於那裏。按照@ bbum的建議打開NSZombies - 這將幫助你找出哪個指針是問題。

0

我想我知道發生了什麼事。你的路徑字符串在findGamePath方法中失去了作用域。

你是否在該方法內分配了路徑字符串並返回它?

一旦它失去了範圍它從內存中釋放。

嘗試訪問該字符串將導致EXC_BAD_ACCESS正在訪問已發佈的對象。

你可以在這裏發佈findGamePath代碼嗎?這可能會更清楚地說明這個問題。

哦,這與線程安全相關的內存管理更多。

+0

謝謝,這裏是findGamesPath代碼 – 2011-04-17 21:51:42

+0

- (NSString *)findGamesPath {NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * documentFolder = [paths objectAtIndex:0]; NSString * result = [documentFolder stringByAppendingPathComponent:@「iShotTrackGames.plist」]; 返回結果; } – 2011-04-17 21:51:59