gpt4 book ai didi

ios - 方法内存泄漏

转载 作者:行者123 更新时间:2023-11-28 23:04:43 25 4
gpt4 key购买 nike

我在从数据库读取数据的方法中遇到了内存泄漏。如果我没有理解错的话,整个邪恶都存在于这个特定的字符串中:

enter image description here

整个方法列表:

-(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];
}

Another image

我用分析器检查了我的代码,它说我没有问题。

请帮我解决这个漏洞。

最佳答案

sqlNominals 有漏洞。

sqlite3_mprintf()sqlite3_vmprintf() 例程将它们的结果写入从 sqlite3_malloc() 获得的内存中。这两个例程返回的字符串应该由 sqlite3_free() 释放。

关于ios - 方法内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9509021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com