在我的應用程序中,我有一個到sqlite3數據庫的連接。我做了一個包裝類,在這個包裝類中我有一個NSMutableDictionary和NSMutableArray。自定義SQLite包裝器中的內存泄漏
每次運行查詢時,我都會從dictonary和rapper類中的數組中運行I(我不會釋放它)。然後將查詢結果添加到數組和字典中。該字典包含另一個子句。
我有一個tableViewController,在這個類我用我的說唱類從數據庫中獲取數據,並將其複製到我的tableviewcontroller變量:
.H
@interface BrandViewController : UIViewController
<UITableViewDataSource , UITableViewDelegate>
{
FairPriceDatabaseView *FairPriceDB;
NSArray *brandsIDs;
NSMutableDictionary *brandsRecords;
UITableView *tableView;
}
.M
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self loadBrandsIDs];
[tableView reloadData];
}
- (void)dealloc {[brandsRecords release];
[brandsIDs release];
[super dealloc];
}
-(NSArray *) loadBrandsIDs
{
[self loadBrandsDB];
[brandsIDs release];
brandsIDs = [[FairPriceDB getBrandIDs]copy];
[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;
}
當測試我得到內存泄漏的星號(brandsRecords = [[FairPriceDB getBrandIDs_NSDictionary]copy];
) 內存泄漏時,我改變tableviewcontroller並回到這些tableviewcontrollers發生....
我想知道,我做這個正確的方式?爲什麼會有泄漏?
另外,每當我發佈一個NSMutableDictionary
,我是否還需要釋放其中包含或不包含的子字典?
FairPriceDataBaseViewController.h(包裝類)
@interface FairPriceDatabaseView {
NSMutableArray * idList;
NSMutableDictionary * recordList;
}
FairPriceDataBaseViewController.m(包裝類)
- (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];
}
-(FairPriceDatabaseView *) initWithFairPriceDatabaseViewFilename: (NSString *) fn
{
if((self = (FairPriceDatabaseView *) [Super initWithDBFilename:fn]))
{
idList = [[NSMutableArray alloc] init];
recordList = [[NSMutableDictionary alloc]init];
}
[self setDefaults];
return self;
}
您可以發佈getAllrecordByRecords的代碼嗎? – zpasternack
那麼,這甚至是你的真實代碼,或者你記得它是什麼?另外,調用實例變量「NSArray ...」並不是一個好習慣。變量名應該以小寫字母開頭,當然不應該與框架類的名字空間衝突。 – jrturton
所以sry,因爲這是我第一次在窗體上發佈一個問題,現在我添加所有真正的代碼,我也有一個說唱歌手類... – Danial