我正在尝试使用 mysql 连接从我的数据库中获取一个 std::string
这是简单的代码:
sql::Statement *stmt = con->createStatement();
sql::ResultSet *res = stmt->executeQuery("SELECT * FROM test.new_table");
while (res->next()) {
std::string somestring = res->getString("idnew_table");
} //crashes here
delete res;
delete stmt;
所以,executeQuery 没问题,我进入循环,如果我中断,预期的结果在某个字符串中。在 somestring 声明之后,我向前推进到循环的末尾,它在下一次迭代之前崩溃了!
这是调用堆栈:
> msvcp100d.dll!std::_Lockit::_Lockit(int kind) Line 64 + 0x14 bytes C++
msvcp100d.dll!std::_Container_base12::_Orphan_all() Line 200 C++
CM.dll!std::_String_val<char,std::allocator<char> >::~_String_val<char,std::allocator<char> >() Line 478 + 0xb bytes C++
CM.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> >() Line 755 + 0xf bytes C++
CM.dll!DAL::GetInfo() Line 45 + 0xc bytes C++
输出:
First-chance exception at 0x1038ad4a (msvcp100d.dll) in CMLauncher.exe: 0xC0000005: Access violation reading location 0xccccccd0.
Unhandled exception at 0x76f615de in CMLauncher.exe: 0xC0000005: Access violation reading location 0xccccccd0.
First-chance exception at 0x76f5016e in CMLauncher.exe: 0x00000000: The operation completed successfully.
Unhandled exception at 0x76f615de in CMLauncher.exe: 0x00000000: The operation completed successfully.
所以看起来我在 C++ 运行时库的某处有一些未初始化的内存... 它看起来像是在 std::string 析构函数中崩溃,这是有道理的,因为它在字符串范围完成时崩溃......
我最好的猜测是 libmysql 使用的是旧版本的 C++ 运行时(比如 msvcp90d.dll)并且它与新版本发生冲突...这有意义吗?
我在 windows 7 下,使用 mySQL Server 5.5,VS2010 Pro。全部为 32 位。谢谢!我很乐意发布任何需要的 mroe 信息。
编辑:在其他人阅读 DumbCoders 评论之前: MySQL Connector example 文档指定必须删除语句和结果集。
最佳答案
这个问题看起来和你的一样here .
关于c++ - std::string 在迭代 mySQL 连接器查询结果时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11055401/