2010-02-13 109 views
0

我得到一個NSArray,它被填充到我的UITableViewController的init方法中。對象崩潰應用程序

我在「didSelectRowAtIndexPath」中使用此對象來推送另一個tableviewcontroller。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
ablogSingleCatTableViewController *singleCatTableViewController = [[ablogSingleCatTableViewController alloc] initWithStyle:UITableViewStylePlain category:[categories objectAtIndex:indexPath.row]]; 
[[self navigationController] pushViewController:singleCatTableViewController animated:YES]; 
[singleCatTableViewController release]; 
} 

這個工作幾次,當我開始我的申請。在選擇一行並返回到rondom點的主uitableview控制器後,我的應用程序在選擇行後崩潰。

我發現一些nslogs,它崩潰,如果我嘗試使用我的「類別」對象。

所以

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

NSLog(@"before"); 
NSLog(@"cats: %@", categories); 
NSLog(@"after"); 

ablogSingleCatTableViewController *singleCatTableViewController = [[ablogSingleCatTableViewController alloc] initWithStyle:UITableViewStylePlain category:[categories objectAtIndex:indexPath.row]]; 
[[self navigationController] pushViewController:singleCatTableViewController animated:YES]; 
[singleCatTableViewController release]; 

} 

與代碼我的應用程序崩潰後的「前」 ...「後,」一直沒有出現。 我不知道爲什麼我的「類別」對象崩潰我的應用程序?!

我的類別對象是在我的頭文件中定義的,並具有@property(nonatomic,retain)。我合成它並在我的dealloc方法中釋放它。

任何人都有想法?

//編輯:

因爲意見

這裏一些更多的細節,:

調試器控制檯說:「程序接收到的信號:‘EXC_BAD_ACCESS’

我創建類別數組像這樣:

- (void)initCategories { 
NSString *path = [[NSBundle mainBundle] pathForResource:@"Categories" ofType:@"plist"]; 
[self setCategories:[[NSArray alloc] initWithContentsOfFile:path]]; 
    } 

調用此方法在我initwithstyle方法

[self initCategories]; 

我的其他自定義初始化方法看起來是這樣的:

- (id)initWithStyle:(UITableViewStyle)style category:(NSDictionary*)cat { 
if (self = [super initWithStyle:style]) { 
    currentCategory = cat; 

    items = [[NSMutableArray alloc] init]; 

    self.title = [currentCategory objectForKey:@"TITLE"]; 
    //XLog("%@", currentCategory); 
} 
return self; 
} 
+0

你能否提供關於碰撞的更多細節?如果你調試應用程序而不是運行它(cmd-Y和cmd-R),你通常會讓調試器在崩潰時暫停執行。這樣,您可以檢查確切的回溯並查看當時任何實例變量的值。 – 2010-02-13 19:59:22

+0

顯示你創建分類數組的代碼請 – willcodejavaforfood 2010-02-13 19:59:39

+0

我覺得你在ablogSingleCatTableViewController initWithStyle:category:method中有問題。你能發佈你的代碼嗎? – EEE 2010-02-13 20:55:32

回答

1

好的,第一件事是;

[self setCategories:[[NSArray alloc] initWithContentsOfFile:path]]; 

你在這裏有泄漏。只是使用

categories = [[NSArray alloc] initWithContentsOfFile:path]; 

此處發生崩潰;

currentCategory = cat; 

你必須保留,使用;

currentCategory = [cat retain]; 

這些都是我在發佈代碼中看到的,如果你沒有在程序的其餘部分的任何錯誤的問題,它應該是罰款與這些修復。

+0

令人驚訝的是它保留了我的「貓」後現在適用於我。im新的objective-c和新的語言,其中ih大大保留和釋放對象,所以即時通訊總是有點困惑,不知道什麼時候做這件事情,特別是保留,釋放工程相當好:D 另一件事,爲什麼我有「setCategories」泄漏?我的意思是,我作爲一個屬性獲得類別,所以我得到了一個setter方法。但再次感謝!它解決了。 – choise 2010-02-14 00:20:09

+0

你應該從蘋果的文檔開始,看到這篇文章http://stackoverflow.com/questions/106627/memory-management-in-objective-c,你可以找到有用的鏈接。學習內存管理非常重要,從此開始,編碼將比您想象的更容易。祝你好運。 – EEE 2010-02-14 00:27:44

+0

在「setCategories」中,您已經使用@property(nonatomic,retain)增加了類別bty的保留計數。但是你也有[[NSArray alloc] init ..],這個保留但是永遠不會被釋放,所以你會有泄漏。你可以編寫[self setCategories:[[NSAray alloc] init ...] autorelease],通過這種方式分配的nsarray會自動發佈,但這樣使用沒有意義。實際上你應該只使用self.categories = [NSArray arrayWithContentOfFile:path]; ,這將保留類別的數量並設置內容。 – EEE 2010-02-14 00:34:24

1

如果你創建你的陣列是這樣的:

NSArray *tmpArray = [[NSArray alloc] initWithBlah ...]; 

請確保您使用合成的吸氣劑由它分配使用此代碼:

self.categories = tmpArray; 
[tmpArray release]; 

如果你這樣做:

categories = tmpArray; 
[tmpArray release]; 

實例變量將根本不被保留。

+0

我的方式: 的NSString *路徑= [[一個NSBundle mainBundle] pathForResource:@ 「類別」 ofType:@ 「的plist」]; \t類別= [[NSArray的頁頭] initWithContentsOfFile:路徑]; – choise 2010-02-13 20:06:28

+0

改成self.categories,它會起作用,就像我在我的帖子中所做的那樣, – willcodejavaforfood 2010-02-13 20:39:10

+0

它沒有,hm – choise 2010-02-13 21:20:59