sqlite3内存泄漏

时间:2011-08-05 08:50:59

标签: iphone memory memory-leaks sqlite

我正在开发一个大量使用sqlite3的应用程序,而且我面临着巨大的内存泄漏问题。

每当我调用使用select的方法时,我将对象添加到数组时会出现内存泄漏。

    - (void) makeSitesSelect:(NSString *)dbPath:(NSString *)theSelect {
    allSitesSelect = [[NSMutableArray alloc] init];
    sqlite3 *database;
    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
        const char *sql = [theSelect cStringUsingEncoding:NSASCIIStringEncoding];
        sqlite3_stmt *selectstmt;

        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
            while(sqlite3_step(selectstmt) == SQLITE_ROW) {
                NSMutableArray *selResult = [[NSMutableArray alloc] init]; // <---- Memory leak
                NSInteger selprimaryKey = sqlite3_column_int(selectstmt, 0);
                [selResult addObject:[NSString stringWithFormat:@"%i", selprimaryKey]]; //<--- memory leak

                const unsigned char *chsiteSite = sqlite3_column_text(selectstmt, 1);
                const unsigned char *chsiteCity = sqlite3_column_text(selectstmt, 2);
                const unsigned char *chsiteCountry = sqlite3_column_text(selectstmt, 3);
                const unsigned char *chsiteGPS = sqlite3_column_text(selectstmt, 4);

                if (chsiteSite != NULL) {
                    [selResult addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]]; //<--- memory leak
                }
                else {
                    [selResult addObject:@""];
                }

                if (chsiteCity != NULL) {
                    [selResult addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)]]; //<---- memory leak
                }
                else {
                    [selResult addObject:@""];
                }
                if (chsiteCountry != NULL) {
                    [selResult addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)]]; //<---- memory leak
                }
                else {
                    [selResult addObject:@""];
                }
                if (chsiteGPS != NULL) {
                    [selResult addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)]]; //<---- memory leak
                }
                else {
                    [selResult addObject:@""];
                }
                [allSitesSelect addObject:selResult];
                [selResult release];
            }
            sqlite3_finalize(selectstmt);
        }
        sqlite3_close(database);
    }
    else
        sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}

感谢任何帮助。

谢谢, 最大

2 个答案:

答案 0 :(得分:0)

你的allSitesSelect是一个属性,那么你应该总是使用self.allSitesSelect = [[NSMutableArray alloc] init];并且您有责任在不使用后释放。你也没有发布数据库。

每次进入while循环时都不需要创建selResult

NSMutuableArray selResult*
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
     if(selResult == nil){
         selResult = [NSMutuableArray copy] //Its an autorelease(you don't need
                                             //release explicitly )
     }
}

答案 1 :(得分:0)

你试试这个

if (!allSitesSelect)
{
 allSitesSelect = [[NSMutableArray alloc] init];
}

并在dealloc中释放此数组

[allSitesSelect release];