我已經搜索高低,試圖解決這個內存泄漏,但我無法弄清楚如何讓它消失。我有幾個類用於連接到我的sqlite數據庫並提取信息。目標C類中的內存泄漏iOS
當我使用性能工具運行應用程序時,泄漏,我一直髮現我的所有類都泄漏了內存。
下面是我的代碼。任何幫助解決此泄漏將不勝感激。
tbl_materialsList.h
@interface tbl_materialsList : NSObject {
NSInteger materialListID;
NSString *shoppingListID;
NSString *projectID;
NSString *materialName;
NSString *numberOfUnits;
NSString *purchased;
NSString *totalPrice;
NSString *unitPrice;
}
@property (nonatomic, readonly) NSInteger materialListID;
@property (nonatomic, retain) NSString *shoppingListID;
@property (nonatomic, retain) NSString *projectID;
@property (nonatomic, retain) NSString *materialName;
@property (nonatomic, retain) NSString *numberOfUnits;
@property (nonatomic, retain) NSString *purchased;
@property (nonatomic, retain) NSString *totalPrice;
@property (nonatomic, retain) NSString *unitPrice;
- (id)getDataToDisplay:(NSString *)dbPath :(NSString *)selectStatement;
- (void)saveData:(NSString *)dbPath :(NSString *)selectStatement;
- (id)initWithPrimaryKey:(NSInteger)pk;
tbl_materialsList.m
- (id)getDataToDisplay:(NSString *)dbPath :(NSString *)selectStatement {
// Init the data Array
NSMutableArray *data = [[NSMutableArray alloc] init];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSString *sql = selectStatement; //"select * from tbl_projects";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &selectstmt, NULL) == SQLITE_OK) {
//loop thru and fill the array
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
//reading the results
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
tbl_materialsList *listObj = [[tbl_materialsList alloc] initWithPrimaryKey:primaryKey];
listObj.shoppingListID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
listObj.projectID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
listObj.materialName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
listObj.numberOfUnits = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
listObj.purchased = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];
listObj.totalPrice = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];
listObj.unitPrice = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 7)];
[data addObject:listObj];
[listObj release];
}
}
//release the compiled statment from memory
sqlite3_finalize(selectstmt);
}
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
return data;
}
爲什麼他們需要'複製'而不是'保留'? – 2010-09-10 15:46:39
@Graham - 我聽說的唯一理由是因爲你可能已經指出你的保留在NSMutableString中,然後可以從你的下面修改。複製阻止了這一點。我不確定爲什麼人們專注於NSString的這個問題(它存在於任何保留的引用中)。 – Peter 2010-09-10 15:50:45
我討厭聽起來很愚蠢,但如果我在返回後發佈數據,看起來沒有任何事情發生。當我遍歷代碼時,它仍然有它的所有對象。如果我之前發佈,它會崩潰,因爲我期望。 – 2010-09-10 15:56:58