【问题标题】:memory leaks when opening sqlite database in iOS? [closed]在 iOS 中打开 sqlite 数据库时内存泄漏? [关闭]
【发布时间】:2013-05-01 06:03:46
【问题描述】:

我一直在这一行得到不断的内存泄漏

> if (sqlite3_open([databasePath UTF8String], &databaseHandle) != SQLITE_OK)

。可能是什么原因 ??我什至在打开数据库后关闭了它。但这无济于事。

我的整个方法是

-(void)copyCustomDatabase{
    /** done - @todo Copy db file from app resources */
    @try {
        NGMobileCaptureSingleton * singleton = [NGMobileCaptureSingleton getSharedInstance]; 
        NSString *documentsDirectory = [singleton getAppDocumentDirectory];
        NSString *apkIdStr = [NSString stringWithFormat:@"%d", [NGMobileCaptureSingleton getSharedInstance].apkId];
        NSString *databaseName = [[[[@"ngcapcust_" stringByAppendingString:apkIdStr] stringByAppendingString:@"_"] stringByAppendingString:[NSString stringWithFormat:@"%d", formId]] stringByAppendingString:@".db"];
            NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:databaseName];
//          NSLog(@"copyCustomDatabase Custom Database Path %@", databasePath);
            bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];
            if (!databaseAlreadyExists)
            {
                NSError *error;
                NSFileManager *fileManager = [[NSFileManager defaultManager] init];
                NSString *srcPath = [[[NSBundle mainBundle] resourcePath]  stringByAppendingPathComponent:databaseName];
                [fileManager copyItemAtPath:srcPath toPath:databasePath error:&error];   
            }                
            if (sqlite3_open([databasePath UTF8String], &databaseHandle) != SQLITE_OK)
                {              
                        [self closeDatabase];                            
                        NSLog(@"NGDefaultCustomHelper copyCustomDatabase Error in creating database handle");
        //            } else {
        //                NSLog(@"NGDefaultCustomHelper copyCustomDatabase Database handle created successfully");          
     }   
        } @catch (NSException *exception) {
            NSLog(@"NGDefaultCustomHelper copyCustomDatabase exception : %@", exception);
        } 
    }

- (void)closeDatabase
 {
    sqlite3_close(databaseHandle);

    databaseHandle = NULL;

    tableMap = NULL;

    tableIdMap = NULL;
}

【问题讨论】:

  • 你以后会关闭数据库吗?
  • 1) 你在使用 ARC 吗? 2)看起来很奇怪,你检查数据是否打开。如果未打开,您正在尝试关闭!如果数据库打开会怎样?
  • @MidhunMP 是的,我正在使用 ARC,当数据库打开时没有任何反应,它只是通知查询是否成功执行
  • 我遇到了数百个内存泄漏 malloc 。都指向我在问题中提到的那条线
  • 何时检测到泄漏?显然,您必须等到您的应用程序使用完数据库并关闭它。 (并在您的关闭例程中添加一个 NSLog 以确保它被执行。)

标签: iphone ios ipad memory-management memory-leaks


【解决方案1】:

sqlite3_open([databasePath UTF8String], &databaseHandle)返回databaseHandle,即(according to the docs)“无论打开时是否出错,与数据库连接句柄相关的资源都应该通过传递给sqlite3_close() 不再需要时”。

【讨论】:

  • 我已经通过调用 closedatabase 方法关闭了。还是没用
  • 其实不是。只有当您可以成功打开数据库时,您才可以这样做。但是文档说“无论打开时是否发生错误,与数据库连接句柄关联的资源都应该在不再需要时通过将其传递给 sqlite3_close() 来释放。”
  • 所以无论选项的结果如何,我都应该关闭它?
  • 是的,文档是这么说的。
  • 谢谢,我现在就试试。
【解决方案2】:

原因是你没有用sqlite_close()关闭数据库。

【讨论】:

  • 我正在使用 ARC 并且在我的关闭数据库方法中,我正在关闭数据库。 - (void)closeDatabase{ sqlite3_close(databaseHandle);数据库句柄 = NULL;表映射=空; tableIdMap = NULL; // NSLog(@"NGDefaultCustomHelper closeDatabase"); }
【解决方案3】:

你忘记添加 sqlite_close()

【讨论】:

    猜你喜欢
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    相关资源
    最近更新 更多