MySQL C++连接器内存溢出错误

发布于 2024-11-26 12:57:38 字数 1077 浏览 1 评论 0原文

我一直在尝试使用 MySQL/C++ 连接器包连接到我自己的本地托管 MySQL 数据库。真正给我带来问题的行是:

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

点击两行中的第二行会给我一个内存分配错误。这是调试器的读数。

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

我真的不确定我做错了什么。我认为它可能是连接指针本身,所以我尝试将其转换为您现在看到的 auto_ptr。同样的错误。我已经尝试了连接函数的不同参数,但这似乎也不是问题。有人能解释一下为什么我在程序的早期就遇到内存泄漏问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熟人话多 2024-12-03 12:57:38

当我使用调试模式,与发布连接器 dll 链接时,我遇到了同样的问题。在调试模式下使用debug dll,可能就可以了。

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.

若水微香 2024-12-03 12:57:38

使用 auto_ptr 时必须小心。由于 auto_ptr 具有不寻常的复制语义(复制操作将所有权转移到所指向的对象,而不是复制指针),因此很容易在完成对象之前意外删除该对象。确保你的 auto_ptr<>对象永远不会出现在赋值的右侧,并且您不会将它们作为参数传递给按值获取 auto_ptr 的函数。您没有显示足够的代码来彻底诊断它。

例如

// 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.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文