objective c - Memory leaks while opening the sqlite database -
following methods gets called didfinishlaunchingwithoptions,when run on instruments opendatabase method causing leaks.. please suggest me how 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 showing { nslog(@"databaseopen"); } else { sqlite3_close(database); nslog(@"databasenotopen"); } }
you leaking because you're not calling sqlite3_close(database) when sqlite_ok.
if (sqlite3_open([path utf8string], &database)==sqlite_ok) { nslog(@"databaseopen"); // leak happens here, stuff call sqlite3_close(database), or move out of if/else block. } else { sqlite3_close(database); nslog(@"databasenotopen"); }
Comments
Post a Comment