I have developed an application which used sqlit3 database. I have made a wrapper class, in this wrapper class I have an NSMutableDictionary and NSMutableArray.
Each time a query is run i used removeAllObjects from the dictonary and the array in the wrapper class (I don't release it). I then add the results from the query to the array and dictionary. The dictionary contains another subdictionaries.
I have 2 sub tableViewController, in these classs ,I get data from the database using my wrapper class and copy it to my tableviewcontroller variable,
but when i change the tableViewController, it is cause the leak in memory,
in the
**brandsRecords = [[FairPriceDB getBrandIDs_NSDictionary]copy];**
i marked as ** and bold the memory leak happends
I appreciate if somebody can guide me to improve these classes and solve the memory leak...
Thank you
CustomeViewController.h
Code:
@interface BrandViewController : UIViewController
<UITableViewDataSource , UITableViewDelegate>
{
FairPriceDatabaseView *FairPriceDB;
NSArray *brandsIDs;
NSMutableDictionary *brandsRecords;
UITableView *tableView;
}
CustomeViewController.m
Code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self loadBrandsIDs];
[tableView reloadData];
}
- (void)dealloc {
[super dealloc];
if (brandsRecords) [brandsRecords release];
if (brandsIDs) [brandsIDs release];
if (FairPriceDB) [FairPriceDB release];
}
-(NSArray *) loadBrandsIDs
{
if (!FairPriceDB)
[self loadBrandsDB];
if(brandsIDs)
[brandsIDs release];
brandsIDs = [[FairPriceDB getBrandIDs]copy];
if (brandsRecords)
[brandsRecords release];
**brandsRecords = [[FairPriceDB getBrandIDs_NSDictionary]copy];**
[FairPriceDB release];
FairPriceDB = nil;
return brandsIDs;
}
- (FairPriceDatabaseView *) loadBrandsDB {
if (!FairPriceDB)
FairPriceDB = [[FairPriceDatabaseView alloc] initWithFairPriceDatabaseViewFilename:@"b.db"];
return FairPriceDB;
}
FairPriceDataBaseViewController.h (wrapper class)
Code:
@interface FairPriceDatabaseView {
NSMutableArray * idList;
NSMutableDictionary * recordList;
}
FairPriceDataBaseViewController.m (wrapper class)
Code:
- (NSArray *) getBrandIDs {
NSDictionary * row;
[idList removeAllObjects]; // reset the array
for (row in [self getQuery:@"SELECT productID,brandName FROM product GROUP BY brandName;"])
[idList addObject:[row objectForKey:@"productID"]];
return idList;
}
-(NSDictionary *) getBrandIDs_NSDictionary{
[recordList removeAllObjects];
[idList removeAllObjects];
[self getBrandIDs];
NSNumber * rowid;
for(rowid in [self idList])
[recordList setObject:[self getProductRow:rowid] forKey:rowid];
return recordList;
}
- (NSDictionary *) getProductRow: (NSNumber *) rowid {
self.tableName = @"select * from product where productID = ?";
return [self getRow:rowid];
}