sqlite3_column_text非常慢 (C) 并产生内存泄漏
sqlite3_column_text is very slow (C) and creates memory leak
我正在用C++编写一个程序,我使用SQLite。我的代码:
if (s == SQLITE_ROW) {
int id = 0;
string stem;
id = sqlite3_column_int (selectStmt, 0);
stem = std::string(
reinterpret_cast<const char*>(sqlite3_column_text (selectStmt, 1))
);
if (id > 0)
StemClass *st = new StemClass(id, stem);
row++;
}
sqlite3_column_text非常慢,并且会产生内存泄漏。
- 我怎样才能避免这种情况?
- 还有别的办法吗?
为了提高速度,您可以尝试:
auto const p(reinterpret_cast<const char*>(sqlite3_column_text (selectStmt, 1)));
::std::string stem(p, sqlite3_column_bytes(selectStmt, 1));
对于泄漏,请使用智能指针,例如 ::std::shared_ptr
或 ::std::unique_ptr
。可能是你的new
在泄漏。以前,您创建一个空的::std::string
实例,然后将新::std::string
复制到其中。坏主意。对每个未被智能指针或某些 RAII 方案(如 SCOPE_EXIT
)捕获的new
表达式都持怀疑态度。
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 尽管遵循了规则,内存泄漏在哪里
- 为什么调用堆栈数组会导致内存泄漏
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 使用模板类的自定义列表类型中的内存泄漏
- 为什么以下C++代码中存在内存泄漏?
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- 我是否生成线程并导致内存泄漏?
- 多线程程序中出现意外的内存泄漏
- 为什么此函数会导致内存泄漏?
- 在 C++ 库中使用cythonized python时内存泄漏
- 需要帮助查找内存泄漏
- 瓦尔格林德的内存泄漏使用新的
- 无法找出我的代码中的内存泄漏
- C++ 结构内部的unordered_map会导致内存泄漏问题吗?
- 可视化 使用 VS Code 查找C++应用程序中的内存泄漏
- Shared_ptr双链接列表内存泄漏
- C++ 在类中使用常量引用文本时 O2 内存泄漏