c ++ mysql sqlstring崩溃0xC0000005无法读取内存
我一直在寻找这个错误在几个地方,我发现的唯一可能是一个野生指针的问题,但不知道如何解决。
问题是c + +连接器工作正常(辛勤工作后)连接到MySQL数据库,检索信息,但每次使用字符串或sqlstring类型它崩溃,(所以我不能从记录集中获取信息)具有相同的错误:
当读取0x00445355处的内存位置时,在0x5e477a8b(msvcp90d.dll)访问冲突时不受控制异常。
这里我正在使用的代码:
int main() {
Driver *driver;
Connection *con;
Statement *stmt;
ResultSet *res;
string fld;
char *fldName = "divisa";
int row=1, nrows=0;
char *url, *database, *usr, *pw;
url = DBHOST;
database = DATABASE;
usr = getstr("User: ", -1, 128); // char* getstr(char prompt[], char chReplace, int maxLen);
pw = new char[128];
pw = getstr("Password: ", ' ', 128);
cout << endl;
try {
driver = get_driver_instance();
con = driver->connect(url, usr, pw);
pw = new char[128];
con->setAutoCommit(0);
con->setSchema(database);
stmt = con->createStatement();
res = stmt->executeQuery("SELECT * FROM `estglob`");
while(res->next()) {
fld.assign(res->getString(fldName));
cout << fld << endl;
}
_getch();
cout << endl << "Cleaning up the resources .." << endl;
delete res;
delete stmt;
con -> close();
delete con;
}
catch (SQLException &e) {
cout << endl << "ERROR: SQLException in " << __FILE__;
cout << " (" << __FUNCTION__<< ") on line " << __LINE__ << endl;
cout << "ERROR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode() << ")";
cout << ", SQLState: " << e.getSQLState() << ")" << endl; // <== CRASH here
_getch();
return(EXIT_FAILURE);
}
catch (std::runtime_error &e)
{
cout << "ERROR: runtime_error in " << __FILE__;
cout << " (" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "ERROR: " << e.what() << endl;
_getch();
return(EXIT_FAILURE);
}
return(EXIT_SUCCESS);
}
任何建议/意见将得到很好的接收!
所以这里的错误点的callstack:
msvcr90d.dll!memcpy(无符号字符* dst = 0x0031fa14,无符号字符* src = 0x00445355,无符号长计数= 0x0000000f)Línea314 Asm
msvcr90d.dll!memcpy_s(void * dst = 0x0031fa14,unsigned int sizeInBytes = 0x0000000f,const void * src = 0x00445355,unsigned int count = 0x0000000f)Línea67 + 0x11 bytes C
msvcp90d.dll!std :: char_traits :: _ Copy_s(char * _First1 = 0x0031fa14,unsigned int _Size_in_bytes = 0x0000000f,const char * _First2 = 0x00445355,unsigned int _Count = 0x0000000f)Línea582 + 0x16 bytes C ++
msvcp90d.dll!std :: _ Traits_helper :: copy_s>(char * _First1 = 0x0031fa14,unsigned int _Size = 0x0000000f,const char * _First2 = 0x00445355,unsigned int _Count = 0x0000000f,std :: _ Secure_char_traits_tag __formal = {...}) Línea714 + 0x15字节C ++
msvcp90d.dll!std :: _ Traits_helper :: copy_s>(char * _First1 = 0x0031fa14,unsigned int _Size = 0x0000000f,const char * _First2 = 0x00445355,unsigned int _Count = 0x0000000f)Línea706 + 0x22 bytes C ++
msvcp90d.dll!std :: basic_string,std :: allocator> :: assign(const std :: basic_string,std :: allocator>&_Right =erróneo,unsigned int _Roff = 0x00000000,unsigned int _Count = 0xffffffff)Línea1067 + 0x25字节C ++
msvcp90d.dll!std :: basic_string,std :: allocator> :: assign(const std :: basic_string,std :: allocator>&_Right =erróneo)Línea1052 C ++
testMySQL.exe!wmain(int argc = 0x00000001,wchar_t * * argv = 0x00834a30)Línea61 + 0x8d bytes C ++
testMySQL.exe!__ tmainCRTStartup()Línea579 + 0x19字节C
testMySQL.exe!wmainCRTStartup()Línea399 C.
KERNEL32.DLL!77391194()
[Los marcos siguientes pueden no ser correctos o faltar,no se han carnadosímbolospara kernel32.dll]
ntdll.dll中!77c7b495()
ntdll.dll中!77c7b468()
更改fld.assign(res-> getString(fldName)); 到fld = res-> getString(fldName);
我假设你在这里使用MySql Connector / C ++。
不知道这是否是问题,但通常结果迭代看起来像这样:
res = stmt->executeQuery("SELECT * FROM `estglob`");
while (res->next())
{
cout << "id = " << res->getString(1);
}
你确定结果集中的第一列是否包含字符串数据? 使用列名而不是其索引,您的逻辑更可靠。
cout << "id = " << res->getString("nameOfColumnWithStringData");
另一种可能性是,如果您使用的是二进制MySql连接器/ C ++库,则您使用的是不同于Visual Studio的版本。 如果是这样的话,您可以使用与您的应用程序一起构建的相同VS从源代码构建MySql库。 请在MySql DLL和EXE上使用depends.exe进行检查 - 如果它们引用不同的MSVC * .DLL版本,那么这是一个潜在的问题。
usr = getstr((char*)"User: ", -1, 128);
pw = new char[128];
pw = getstr((char *)"Password: ", ' ', 128);
我不知道gettr是干什么的,但是将一个字符串文字转换为char *是一个直接的信号,表示你将会做一些非常非常错误的事情。 改用std :: string和c_str()成员函数。 我也在研究你的大量删除操作,并在那里感受到坏消息的可能性 - 当你要求泄漏时,你应该使用范围指针来控制内存。
链接地址: http://www.djcxy.com/p/85065.html上一篇: c++ mysql sqlstring crashes 0xC0000005 unable to read memory