我正在使用 SQLite 来存储我的数据。我正在编写包装类,我想知道:如果 (res != SQLITE_OK) 和 errorMsg 将显示到屏幕上,是否会发生内存泄漏??
那么我需要做 free(errorMsg); 吗?在“if”语句中?谢谢!
-(int) executeQuery: (NSString *) sqlQueryStr
{
char *errorMsg = NULL;
int res = SQLITE_ERROR;
res = sqlite3_exec(database, [sqlQueryStr UTF8String], NULL, NULL, &errorMsg);
if (res != SQLITE_OK)
{
sqlite3_close(database);
NSLog(@"executeQuery Error: %@", errorMsg);
database = NULL;
return res;
}
return res;
}
最佳答案
您应该使用sqlite3_free()
来释放错误消息字符串,如 the documentation :
To avoid memory leaks, the application should invoke sqlite3_free() on error message strings returned through the 5th parameter of of sqlite3_exec() after the error message string is no longer needed.
关于iphone - sqlite3_exec是否存在内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4156962/