我在我的應用程序中找不到內存泄漏的原因。我發現通過儀器存在內存泄漏,而不是更多次我調用函數,而不是發生更多內存泄漏。所以很明顯會發生內存泄漏。涉及NSString的內存泄露
這是我的代碼。 對象:
@interface WordObject : NSObject
{
int word_id;
NSString *word;
}
@property int word_id;
@property (copy) NSString *word;
@end
方法被用於填充的UITableView:
-(void) reloadTableData {
[tableDataArray removeAllObjects];
for (int i = 0; i <= 25; i++)
{
NSMutableArray *words_in_section = [ [NSMutableArray alloc] init];
[tableDataArray addObject:words_in_section];
[words_in_section release];
}
WordObject *tempWordObj;
int cur_section;
while (tempWordObj = [ [WordsDatabase sharedWordsDatabase] getNext])
{
cur_section = toupper([ [tempWordObj word] characterAtIndex:0 ]) - 'A';
NSMutableArray *temp_array = [tableDataArray objectAtIndex:cur_section];
[temp_array addObject:tempWordObj];
[tableDataArray replaceObjectAtIndex:cur_section withObject:temp_array];
}
[mainTableView reloadData];
[mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
}
的細胞獲得內容:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
}
WordObject *tempWordObj = [ [tableDataArray objectAtIndex:[indexPath section] ] objectAtIndex:[indexPath row] ];
if (!tempWordObj)
{
return cell;
}
cell.text = [tempWordObj word];
return cell;
}
這裏是我如何清理內存:
-(void) freeMemory
{
if (tableDataArray)
{
[tableDataArray release];
tableDataArray = nil;
}
}
正在從reloadTableData調用的函數:
-(WordObject*) getNext {
if(sqlite3_step(getStmt) == SQLITE_DONE)
{
sqlite3_reset(getStmt);
return nil;
}
WordObject *tempWord = [ [WordObject alloc] init];
tempWord.word_id = sqlite3_column_int(getWordsStmt, 0);
tempWord.word = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getWordsStmt, 1)]; //Here a leak occurs
return [tempWord autorelease];
}
而泄漏的對象是[WordObject word]。
我會非常感謝任何能夠幫助我解決這個問題的人。
你在WordObject的dealloc中釋放單詞嗎? – 2009-04-08 22:09:06