我在这一行得到了持续的内存泄漏
> 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;
}
答案 0 :(得分:1)
sqlite3_open([databasePath UTF8String], &databaseHandle)
返回databaseHandle
,其中(according to the docs)“打开时是否发生错误,与数据库连接句柄关联的资源应该在不再需要时将其传递给sqlite3_close()
即可发布。
答案 1 :(得分:0)
原因是您没有使用sqlite_close()
关闭数据库。
答案 2 :(得分:0)
你忘了添加sqlite_close()