Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leaks while opening the sqlite database

Following methods gets called from didFinishLaunchingWithOptions,when I run on Instruments openDatabase method causing leaks.. please suggest me how to clear

- (NSString*)getdestinationPath {
    NSArray *pathsArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];
    NSString *destinationPath=[doumentDirectoryPath stringByAppendingPathComponent:dataBaseName];
    NSLog(@"database path %@",destinationPath);
    return destinationPath;
}




- (void)chkAndCreateDatbase {
    NSFileManager *fileManger=[NSFileManager defaultManager];
    NSError *error;
    NSString *destinationPath=[self getdestinationPath];
    if ([fileManger fileExistsAtPath:destinationPath]){
        //NSLog(@"database localtion %@",destinationPath);
        return;
    }
    NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:dataBaseName];
    [fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];
}





- (void)openDatabase {
    path=[self getdestinationPath];
    if (sqlite3_open([path UTF8String], &database)==SQLITE_OK) // here leak is showing
    {
        NSLog(@"dataBaseOpen");
    }
    else {
        sqlite3_close(database);
        NSLog(@"dataBaseNotOpen");  
    }   
}
like image 610
Ayesha Fatima Avatar asked Jul 04 '26 22:07

Ayesha Fatima


1 Answers

You are leaking because you're not calling sqlite3_close(database) when you get SQLITE_OK.

if (sqlite3_open([path UTF8String], &database)==SQLITE_OK) 
{
    NSLog(@"dataBaseOpen");
    // leak happens here, do stuff then call sqlite3_close(database), or move it out of the if/else block.

}
else {
    sqlite3_close(database);
    NSLog(@"dataBaseNotOpen");  
}   
like image 67
David Avatar answered Jul 06 '26 12:07

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!