我在我的應用程序中有3個內存泄漏,我找不到如何修復它。我對xcode和目標c很陌生。這裏是我有的代碼:修復內存泄漏的問題iphone
if(sqlite3_prepare_v2([[DatabaseController sharedDatabaseController] getDb], sqlQueryConverted, -1, &dbStatement, NULL)==SQLITE_OK){
//Run the query
while (sqlite3_step(dbStatement) == SQLITE_ROW)
{
const char *name = (const char *)sqlite3_column_text(dbStatement, 0);
int courseId = sqlite3_column_int(dbStatement, 1);
const char *location = (const char *)sqlite3_column_text(dbStatement, 2);
const char *date = (const char *)sqlite3_column_text(dbStatement, 3);
//Convert the returnedElement char to string
nameConverted = [[NSString alloc] initWithUTF8String:name];
locationConverted = [[NSString alloc] initWithUTF8String:location];
dateConverted = [[NSString alloc] initWithUTF8String:date];
Course *course = [[[Course alloc]initWithName:nameConverted _id:courseId location:locationConverted courseDate:dateConverted] autorelease];
//Add the course to the to a temporary list to remove duplicated items
[tempResults addObject:course];
}
[nameConverted release];
[locationConverted release];
[dateConverted release];
}
我試圖autorelease它也是。此代碼用於過濾搜索並重新加載搜索顯示錶。如果我把release
行寫入while
聲明中,如果我輸入2個字母,應用程序就會崩潰。我怎麼能解決這個問題?
謝謝。
編輯:我一直在這個問題來回奔跑,沒有運氣。我得出的結論是,儀器出現問題,因爲它仍然顯示內存泄漏。下面的代碼因爲它是今天,當我認爲應該解決這個問題:
NSString *nameConverted = [[NSString alloc] initWithUTF8String:name];
NSString *locationConverted = [[NSString alloc] initWithUTF8String:location];
NSString *dateConverted = [[NSString alloc] initWithUTF8String:date];
Course *course = [[[Course alloc]initWithName:nameConverted _id:courseId location:locationConverted courseDate:dateConverted] autorelease];
//Add the course to the to a temporary list to remove duplicated items
[tempResults addObject:course];
course = nil;
[course release];
[nameConverted release];
nameConverted = nil;
[locationConverted release];
locationConverted = nil;
[dateConverted release];
dateConverted = nil;
NSLog(@"course retain count %i",[course retainCount]);
NSLog(@"name coverted retain count %i",[nameConverted retainCount]);
NSLog(@"location coverted retain count %i",[locationConverted retainCount]);
NSLog(@"date coverted retain count %i",[dateConverted retainCount]);
的日誌告訴我,retainCount = 0;
,所以我不明白爲什麼有內存泄漏。你們能給我一些建議嗎?
再次感謝。
course = nil; [課程發佈]; 是泄漏。通過將它設置爲零,即使沒有釋放它也會丟失物體。只需使用[課程發佈]即可發佈。 – trapper