gpt4 book ai didi

ios - 使用 sqlite 时出现 6MB 内存泄漏

转载 作者:行者123 更新时间:2023-12-03 03:25:14 24 4
gpt4 key购买 nike

当我使用 sqlite 时,我有 6MB 内存泄漏。现在我正在测试 getBookWithIdTest 方法。

-(IBAction) onTest:(id)sender
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
for (int i=0; i<100; i++)
{
[DatabaseManager getBookWithIdTest:i];
}
[myPool drain];
}

我有 6 MB 内存泄漏。但为什么呢?

+ (BookSettings *)getBookWithIdTest:(int)abookId
{
BookSettings *book = [[[BookSettings alloc] init] autorelease];
sqlite3 *database;

if(sqlite3_open([DatabaseManager databasePath], &database) == SQLITE_OK)
{
sqlite3_stmt *compiledStatement;

//FIRST PART
const char *sqlStatementBook = [[NSString stringWithFormat:@"SELECT * FROM t_abooks"] cStringUsingEncoding:NSASCIIStringEncoding];

if(sqlite3_prepare_v2(database, sqlStatementBook, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
}
}
else NSLog(@"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database));

sqlite3_reset(compiledStatement);
//END FIRST PART
// SECOND PART
const char *sqlStatementAuthors = [[NSString stringWithFormat:@"SELECT * FROM t_authors"] cStringUsingEncoding:NSASCIIStringEncoding];

if(sqlite3_prepare_v2(database, sqlStatementAuthors, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
}
}
else NSLog(@"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database));

sqlite3_reset(compiledStatement);
//END SECOND PART
sqlite3_finalize(compiledStatement);
} else NSLog(@"sqlite3_open error");

sqlite3_close(database);
return book;
}

但是如果我删除第一部分或第二部分,我就不会出现泄漏。例如

+ (BookSettings *)getBookWithIdTest:(int)abookId
{
BookSettings *book = [[[BookSettings alloc] init] autorelease];
sqlite3 *database;

if(sqlite3_open([DatabaseManager databasePath], &database) == SQLITE_OK)
{
sqlite3_stmt *compiledStatement;

//FIRST PART -removed
// SECOND PART
const char *sqlStatementAuthors = [[NSString stringWithFormat:@"SELECT * FROM t_authors"] cStringUsingEncoding:NSASCIIStringEncoding];

if(sqlite3_prepare_v2(database, sqlStatementAuthors, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
}
}
else NSLog(@"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database));

sqlite3_reset(compiledStatement);

sqlite3_finalize(compiledStatement);
} else NSLog(@"sqlite3_open error");

sqlite3_close(database);
return book;
}

最佳答案

我解决了这个问题。我需要添加

sqlite3_finalize(compiledStatement);

到第一部分的结尾。

关于ios - 使用 sqlite 时出现 6MB 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9454099/

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