Mar*_*ein 1 c++ memory stl vector
这是在双核32位Vista机器上的Visual Studio 2008上.在调试代码中运行正常,但在发布模式下这个炸弹:
void getFromDB(vector<string>& dates) {
...
sql::Resultset res = stmt->executeQuery("SELECT FROM ...");
while (res->next()) {
string date = res->getString("date");
dates.push_back(date);
} // <<< crashing here (line 56)
delete res;
}
Run Code Online (Sandbox Code Playgroud)
MySQL C++连接器在它的ResultSet中有这个方法:
virtual std::string getString(const std::string& columnLabel) const = 0;
Run Code Online (Sandbox Code Playgroud)
由于某些原因,在编译版本中(针对MySQL C++连接器DLL),这会在循环结束时因堆损坏而崩溃:
HEAP [sa-ms-release.exe]:为RtlFreeHeap指定的地址无效(024E0000,001C4280)Windows在sa-ms-release.exe中触发了断点.
ntdll.dll!_RtlpBreakPointHeap@4() + 0x28 bytes
ntdll.dll!_RtlpValidateHeapEntry@12() + 0x713e8 bytes
ntdll.dll!_RtlDebugFreeHeap@12() + 0x9a bytes
ntdll.dll!@RtlpFreeHeap@16() + 0x145cf bytes
ntdll.dll!_RtlFreeHeap@12() + 0xed5 bytes
kernel32.dll!_HeapFree@12() + 0x14 bytes
> sa-ms-release.exe!free(void * pBlock=0x001c4280) Line 110 C
sa-ms-release.exe!std::allocator<char>::deallocate(char * _Ptr=0x001c4280, unsigned int __formal=32) Line 140 + 0x9 bytes C++
sa-ms-release.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Tidy(bool _Built=true, unsigned int _Newsize=0) Line 2158 C++
sa-ms-release.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> >() Line 907 C++
sa-ms-release.exe!StyleData:: getFromDB( std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > > & dates) Line 56 + 0x69 bytes C++
Run Code Online (Sandbox Code Playgroud)
我想我可能违反了一些基本的C++规则,导致析构函数被调用在我希望保留在向量中的字符串上.
如果我更换
string date = res->getString("date")
Run Code Online (Sandbox Code Playgroud)
同
string date = string ("2008-11-23");
Run Code Online (Sandbox Code Playgroud)
一切正常.它似乎与MySQL C++ Connector getString()方法返回的字符串有关.我想,返回的字符串会被破坏并导致问题.但更可能的是其他错误.我正在使用release(opt)MySQL连接器.
imho,问题可能来自VS运行时库.这意味着,你用于sql连接器方法的DLL不是用"多线程dll"代码生成选项编译的.所以不同版本的字符串通过参数传递并且它们chrash.I认为你应检查此代码生成标志.