3

I'm trying to get a std::string from my database using mysql connexion

Here is the simple code:

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;

So, the executeQuery is fine, I enter the loop, and if I break, the expected results are in somestring. After the somestring declaration, I step foward to the end of the loop, and it crashes before the next iteration!

Here is the call stack:

>   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++

Output:

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.

So it looks like I have some uninitialized memory somewhere in the C++ runtime lib... It look like it's crashing in the std::string destructor, which kind of makes sense because it crashes when the scope of the string is done...

My best guess is that libmysql is using an older version of the C++ runtime (say msvcp90d.dll) and that it's clashing with the new one... does that even make sense?

I'm under windows 7, using mySQL Server 5.5, VS2010 Pro. all in 32bits. thx! I'll be happy to post any mroe information that is needed.

Edit: Before anyone else reads DumbCoders comment: MySQL Connector example The documentation specifies that both the statement and the resultSet have to be deleted.

David Menard
  • 2,261
  • 3
  • 43
  • 67
  • 3
    Why are you using delete on objects not allocated by new ?? You should not be using delete if you haven't used new to allocate memory. – DumbCoder Jun 15 '12 at 17:21
  • 3
    because the mysql-connexion documentation specifies that I have to. Anyways, it crashes before the deletes. This comment was way too fast for you to have time to have read the whole thing. – David Menard Jun 15 '12 at 17:22
  • Did you debug it ? And way too fast, that is not because you are doing something which is wrong. – DumbCoder Jun 15 '12 at 17:27
  • 1
    I did debug, that's why there is a callstack and I say "stepforward". Not wrong. http://dev.mysql.com/tech-resources/articles/mysql-connector-cpp.html. Please stop posting. – David Menard Jun 15 '12 at 17:29
  • Why are you creating somestring in loop? It wouldn't be visible outside. – Protheus Jun 15 '12 at 17:31
  • @ Protheus: There is no real reason why. Originally, I did create somestring before the loop (it was a vector of strings), but it would crash, so I put it there for the example code to be simpler. – David Menard Jun 15 '12 at 17:32
  • 1
    @DavidMenard Do `stmt` and `res` pointers get legal values? Try printing something inside the loop to check if it enters it at all. – Eitan T Jun 15 '12 at 17:34
  • @EitanT They do. It does. "somestring" has the expected value when I enter the loop. – David Menard Jun 15 '12 at 17:35
  • 2
    @DumbCoder obviously the pointers are `new`ed inside the library functions. If the example in the official documentation shows that this is how it is supposed to be used, then this is how it is supposed to be used, no matter how awkward the code looks. – Eitan T Jun 15 '12 at 17:36
  • @EitanT - I am not complaining on the code part, I am just pointing out understand before you write and give proper reasons when you believe the othe person is wrong. – DumbCoder Jun 15 '12 at 17:40

1 Answers1

5

This problem seems just like yours here.

Community
  • 1
  • 1
andre
  • 7,018
  • 4
  • 43
  • 75
  • I have run into the before, linking to a library with a slightly different version of `std::vector` (`_SECURE_SCL` was set, mine was not). Very frustrating. VS2010 gives you a clear error on this iirc – Ed S. Jun 15 '12 at 17:50
  • So, this is in fact the same question. But I am still confused about the actual solution. I can't recompile the mysql-connector lib, so how do I use it appropriately? Ed S. did you ever get around it? – David Menard Jun 15 '12 at 17:51
  • 2
    @DavidMenard: You have to match their settings. – Ed S. Jun 15 '12 at 17:53
  • 1
    For anyone in the future who encounters this particular problem with mysql connector/C++: I ended up trying compiling with cmake, but then entry points were no where to be found. I wiped my plate clean and reinstalled everything, and what ended up working was connector v.1.1.0, the release build of the lib and dll (in the "opt" folder) linked with the libmysql.dll from the MySQL server pack. thx again for the help :D Warning: version 1.1.0 .msi pack is missing sqlstring.h, which you can get from the archive (.zip) – David Menard Jun 15 '12 at 19:52