2

I've been trying to hook up to my own locally hosted MySQL database with the MySQL/C++ Connector package. The lines that are really giving me a problem are:

driver = get_driver_instance();
auto_ptr < Connection > con (driver -> connect("tcp://127.0.0.1:3306", "root", "root"));

Hitting the 2nd of the two lines gives me a memory allocation error. Here's the readout from the debugger.

HEAP[mySQLTestApp.exe]: Invalid allocation size - CCCCCCD0 (exceeded 7ffdefff)
First-chance exception at 0x7c812afb in mySQLTestApp.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012fa88..
HEAP[mySQLTestApp.exe]: Invalid allocation size - CCCCCCCD (exceeded 7ffdefff)
First-chance exception at 0x7c812afb in mySQLTestApp.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012f428..
First-chance exception at 0x7c812afb in mySQLTestApp.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
Unhandled exception at 0x7c812afb in mySQLTestApp.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012f428..

I'm really not sure what I'm doing incorrectly. I thought it might have been the Connection pointer itself, so I tried converting it into the auto_ptr that you see now. Same error. I've tried different parameters for the connect function, but that doesn't seem to be the problem as well. Can anybody explain why I'm having memory leak problems so early in the program?

Mister Mister
  • 33
  • 1
  • 8
  • Uninitialized memory in debug mode is [usually filled with CCCCCCCC](http://stackoverflow.com/q/370195/576139) by MSVC. It looks like something somewhere is using an uninitialized value. – Chris Eberle Jul 25 '11 at 19:56
  • Any chance you can get an actual stack trace? – Chris Eberle Jul 25 '11 at 19:58
  • does the driver need some secondary initialization before you can use it? – ethrbunny Jul 25 '11 at 19:59
  • So the only thing that could cause this error is the driver, correct? all of the parameters are constants, and the connection hasn't been established yet. But I set a breakpoint before and after the driver = get_driver_instance() line, and it seems to be setting to a place in memory. – Mister Mister Jul 25 '11 at 20:00
  • As far as a more initialization goes, this is straight from the dev guide example, so if there's some functionality that I'm not calling, then it's not mentioned in the official examples. As far as memory goes, here's what driver points to over time. Before get_driver_instance:0xcccccccc (That's to be expected. We haven't initialized it yet). After get_driver_instance: 0x003d55a8. This is clearly pointing to something now, I just don't know what. – Mister Mister Jul 25 '11 at 20:17
  • Two lines are not sufficient to know what's going on. Make a testcase. – Lightness Races in Orbit Sep 08 '11 at 09:06

2 Answers2

4

I have got the same problem,when I use debug mode, linking with release connector dll. Use debug dll when in debug mode, may be OK.

Peter
  • 41
  • 3
1

You have to be careful when using auto_ptr. Because auto_ptr has unusual copy semantics (a copy operation transfers ownership to the pointed to object, rather than copies the pointer), it is quite easy to accidently delete an object before you were finished with it. Make sure that your auto_ptr<> objects never appear on the right hand side of an assignment, and that you don't pass them as arguments to a function that takes an auto_ptr by value. You don't show enough of your code to thoroughly diagnose it.

For example

// declaration for some function you've defined later
void some_user_function(auto_ptr<Connection> con);

auto_ptr<Connection> con(driver->connect("tcp://127.0.0.1:3306", "root", "root"));
some_user_function(con);
// At this point con will be a NULL pointer, and the Connection object it used to point
// to will have been deleted.
Stephen C. Steel
  • 4,380
  • 1
  • 20
  • 21