【问题标题】:sqlite3 memory leakssqlite3 内存泄漏
【发布时间】:2011-10-20 16:32:09
【问题描述】:

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

每次我调用进行选择的方法时,当我将对象添加到数组时都会发生内存泄漏。

    - (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.
}

感谢任何帮助。

谢谢, 最大

【问题讨论】:

  • 尝试将其分配到while循环之外....tat is b4 while-loop
  • quote: “我正在开发一个大量使用 sqlite3 的应用程序”...你考虑过 Core Data 吗?您应该尝试一下,它简单易学且非常高效!
  • @booleanBoy:我无法在 while 循环之外分配 selResult,因为我必须在每个“圈”处清空 e 重新初始化它。
  • daveoncode: 可能很好,但我正在更新一个已经发布的应用程序,我会添加数千行从 sqlite 移动到 CoreData 没有问题,因为所有用户已经使用我的应用程序。
  • 为什么那行是内存泄漏? [selResult addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]]; //

标签: iphone memory memory-leaks sqlite


【解决方案1】:

你试试这样

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

并在 dealloc 中释放这个数组

[allSitesSelect release];

【讨论】:

    【解决方案2】:

    你的 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 )
         }
    }
    

    【讨论】:

    • 即使问题不在于 allSitesSelect 而在于 selResult,我还是按照您的建议进行了操作(allSitesSelect 已在 dealloc 方法中发布)。关于发布数据库,如果我执行 [数据库发布],它会返回一个带有 sqlite3* 的错误
    • @masgar 我已经改进了我的答案以消除您的疑问,希望如此。
    猜你喜欢
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2013-01-20
    • 2011-10-31
    相关资源
    最近更新 更多