我从db中读取数据的方法中有内存泄漏。如果我理解得对,整个邪恶都存在于这个特定的字符串中:
整个方法列表:
-(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;
}
最后,当另一个类中的viewDidLoad使用此方法时:
·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];
}
我用分析器检查了我的代码,它说我没有问题。
请帮我解决这个漏洞。
答案 0 :(得分:2)
sqlNominals
发生泄密。
sqlite3_mprintf()
和sqlite3_vmprintf()
例程将其结果写入从sqlite3_malloc()
获取的内存中。这两个例程返回的字符串应由sqlite3_free()
释放。