-2

我在这条线上一直有内存泄漏

> 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;
}
4

3 回答 3

1

sqlite3_open([databasePath UTF8String], &databaseHandle)返回databaseHandle,其中(根据文档)“无论打开时是否发生错误,与数据库连接句柄关联的资源都应通过将其传递给sqlite3_close()不再需要时释放”。

于 2013-05-07T10:19:58.550 回答
0

原因是您没有使用sqlite_close().

于 2013-05-07T10:18:56.567 回答
0

你忘了添加 sqlite_close()

于 2013-05-07T10:23:42.483 回答