我正面臨一個有關NSMutableArray的奇怪問題。我搜索了幾個類似的帖子,但沒有luck.The情況是這樣的: 在我的appDelegate我宣佈一個NSMutableArray屬性是這樣的:NSMutableArray:發送到已釋放映像的消息
@property (nonatomic, retain) NSMutableArray *myRequests;
然後在同一個類的方法,我初始化數組是這樣的:
self.myRequests = [[[NSMutableArray alloc] init] autorelease];
然後,在用於予對象添加到陣列的方法相同的方法的循環:
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
MyRequest *req = [MyRequest alloc];
req.s_prp= [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
.
.
.
[self.myRequests addObject:req];
[req release];
}
我使用這個數組喂用數據一個UITableView。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Cell initialization ...
MyRequest *req = (MyRequest *) [delegate.myRequests objectAtIndex:indexPath.row] ;
cell.stypeLabel.text = req.ser_type_name; // RANDOMLY OCCURING ERROR !!!!!!!!!!!
}
異常隨機發生並且該消息表示:[MyRequest ser_type_name]:消息發送到釋放的實例0x6e7af20
有什麼問題與上面的代碼?? MyRequest是一個自定義類。我正在使用自定義tableviewcell類。
MyRequest類很簡單。它有幾種屬性,形式如下:
@property (retain,nonatomic) NSString *ser_type_name;
和釋放對象的dealloc方法。
感謝
我沒有看到任何會出錯的東西......但是我建議你正確地實現/調用/使用[MyRequest初始化]方法。只有做一個分配可能沒有預期的效果。 (這是init的目的)。 – 2011-12-11 19:28:23
錯誤不在您發佈的代碼中。它可以是任何你使用'req'或'ser_type_name'的地方 - 尤其是你釋放任何地方的任何地方。 – grahamparks