NCFUSN
NCFUSN

Reputation: 1654

Memory leak in method

I've got a memory leak in method that reads data from db. If I do understand right, the whole evil lives in this particular string:

enter image description here

whole method listing:

-(NSMutableArray*)returnNominals:(int)subCountryID
{
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path =
[documentsDirectory stringByAppendingPathComponent:databaseName];

NSMutableArray *nominals=[[[NSMutableArray alloc]init]autorelease];

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{

const char* sqlNominals=sqlite3_mprintf("SELECT noms.nominalID, noms.nominal,noms.nominalName,rel.nominalImg,noms.priority\
FROM nominals AS noms\
INNER JOIN NominalsAndSubCountriesRelation as rel\
ON noms.nominalID=rel.NominalID\
WHERE rel.SubcountryID=%i\
ORDER BY noms.priority",subCountryID);

       sqlite3_stmt *statement;
int sqlResult = sqlite3_prepare_v2(database, sqlNominals, -1, &statement, NULL);

if ( sqlResult== SQLITE_OK)
{
    while (sqlite3_step(statement) == SQLITE_ROW)
    {
        Nominal *nom=[[Nominal alloc]init];
        nom.nominalID=sqlite3_column_int(statement, 0);
        char *nominal=(char *)sqlite3_column_text(statement, 1);
        char *nominalName=(char*)sqlite3_column_text(statement, 2);
        char *nominalImg=(char*)sqlite3_column_text(statement, 3);
        nom.nominal=(nominal)?[NSString stringWithUTF8String:nominal]: @"";
        nom.nominalName=(nominalName)?[NSString stringWithUTF8String:nominalName]: @"";
        nom.nominalImg=(nominalImg)?[NSString stringWithUTF8String:nominalImg]: @"noimg";
        [nominals addObject:nom];
        [nom release];
    }
    sqlite3_finalize(statement);
}
}
else
{
    [self dbConnectionError];
}
    return nominals;
}

And finally when viewDidLoad in another class uses this method:

.h

    @interface Nominals : UIViewController 
   {
     NSMutableArray *nominalsArr;
     NSInteger subCountryID;
   }
   @property(nonatomic,retain)NSMutableArray *nominalsArr;
   @property(nonatomic)NSInteger subCountryID;

.m

- (void)viewDidLoad
{
[super viewDidLoad];
 [[self navigationController]setToolbarHidden:YES animated:YES];
    DBAccess *dbAccsess=[[DBAccess alloc]init];
    self.nominalsArr=[dbAccsess returnNominals:subCountryID];
    [dbAccsess closeDataBase];
    [dbAccsess release];
}
- (void)dealloc
{
[tableView release];
[searchBar release];
[_toolBar release];
[nominalsArr release];
[searchController release];
[filteredItems release];
[super dealloc];
}

Another image

I've checked my code with analyzer and it says I've got no issues.

Please help me to solve this leak.

Upvotes: 0

Views: 313

Answers (1)

fbernardo
fbernardo

Reputation: 10124

You have a leak on sqlNominals.

The sqlite3_mprintf() and sqlite3_vmprintf() routines write their results into memory obtained from sqlite3_malloc(). The strings returned by these two routines should be released by sqlite3_free().

Upvotes: 2

Related Questions