bugMySQL++ - Bugs: bug #17761, Have memory leak then trying to...

Show feedback again

You are not allowed to post comments on this tracker with your current authentification level.

bug #17761: Have memory leak then trying to connect to the database fails

Submitted by:  Dmitry <gz_>
Submitted on:  Fri Feb 18 21:46:54 2011  
Category: NoneSeverity: 4 - Important
Priority: 5 - NormalStatus: Fixed
Privacy: PublicAssigned to: None
Open/Closed: Closed

(Jump to the original submission Jump to the original submission)

Thu Dec 12 15:19:36 2013, comment #6:

Fixed in r2760. Will appear in next release.

Warren Young <wyoung>
Project Administrator
Thu Mar 24 08:16:00 2011, comment #5:

Dmitry showed you this case:

#include <mysql++/mysql++.h>

int main()
while (true)
// Infinitely trying to connect to non-existent mysql server
mysqlpp::Connection conn("Config", "", "user", "123", 123);
// If it will connect successfully, we will do something.
// Insert data in some table for example.
// But it doesn't matter for our bug.
catch (...) { }

// You don't need any finalization since it was infinite loop, but ok, I'll call them to calm you:

You can see memory leak by using any tool, that shows process memory usage. It grows up to 500 MB resident memory for 1 minute working at my PC.

But this snippet is not necessary to show the bug, because you can see it in source code, patched up by Dmitry. When connect() throws exception in constructor of Connection, the memeber pointer driver_ would not freed. Class DBDriver (not MySQL, but MySQL++) doesn't register itself in any type of garbage collector, so it will stay in memory.

Just look at code of second constrcuctor of class Connection and you will see this bug.

Eugene <bunkar>
Wed Mar 23 15:45:25 2011, comment #4:

It's invalid because the posted code snippet doesn't contain a call to Connection::thread_end() and to mysql_library_end(), as the referenced MySQL++ FAQ item tells you to. Since that is the advice given to plug memory leaks and the complaint is about memory leaks, I don't see that this is a valid bug report.

If you think you have something valid, you have to provide a test case that doesn't itself have known problems.

Warren Young <wyoung>
Project Administrator
Wed Mar 23 15:24:34 2011, comment #3:


Warren, why is this bug marked as Invalid? It's widespread bug with lost pointer in constructor after exception.

Does anybody support this library?

Eugene <bunkar>
Sat Feb 19 09:25:38 2011, comment #2:

I am sorry, i am haste.

My sustem is: Linux gz 2.6.35-25-generic #44-Ubuntu
I compiled main.cpp (in attachment) file using gcc 4.4:
g++ main.cpp -I /usr/include/mysql -lmysqlpp

and ran it under valgrind-3.6.0
valgrind --leak-check=full ./a.out

I got (full text in attachment: before.txt):
==8453== LEAK SUMMARY:
==8453== definitely lost: 104,544 bytes in 99 blocks
==8453== indirectly lost: 106,688 bytes in 394 blocks
==8453== possibly lost: 3,168 bytes in 7 blocks
==8453== still reachable: 16 bytes in 1 blocks
==8453== suppressed: 0 bytes in 0 blocks

I used another patch without using tr1 (in attachment: patch.diff)
and got (after.txt):
==8740== LEAK SUMMARY:
==8740== definitely lost: 0 bytes in 0 blocks
==8740== indirectly lost: 0 bytes in 0 blocks
==8740== possibly lost: 0 bytes in 0 blocks
==8740== still reachable: 16 bytes in 1 blocks
==8740== suppressed: 0 bytes in 0 blocks

Sorry if i blind and missed something.

(file #12486, file #12487, file #12488, file #12489)

Dmitry <gz_>
Fri Feb 18 23:42:51 2011, comment #1:

You aren't following the instructions in this FAQ item:


I seriously doubt you've found a real memory leak in MySQL++. It is almost certainly one of the C API memory allocations covered in that FAQ item.

I'm going to leave this bug item open in case you have, but your code snippet isn't valid as-is, since it has known violations of documented advice for plugging memory leaks.

In addition, the patch won't be accepted since it requires TR1 support. There are compilers MySQL++ currently supports without TR1, so you'd need to come up with a more appropriate solution if one were actually needed.

Warren Young <wyoung>
Project Administrator
Fri Feb 18 21:46:54 2011, original submission:

How to repeat:
int main()
while (true)
mysqlpp::Connection conn("Config", "", "user", "123", 123);
catch (...) { }

To fix i used:

Index: connection.cpp
--- connection.cpp (revision 2685)
+++ connection.cpp (working copy)
@@ -64,7 +64,6 @@
- delete driver_;

Index: connection.h
--- connection.h (revision 2685)
+++ connection.h (working copy)
@@ -41,6 +41,7 @@
#include "options.h"

#include <string>
+#include <tr1/memory>

namespace mysqlpp {

@@ -164,7 +165,7 @@
/// the mailing list asking about it. Unless you're doing something
/// very low-level, there should never be a reason to use the
/// driver directly.
- DBDriver* driver() { return driver_; }
+ DBDriver* driver() { return driver_.get(); }

/// \brief Asks the database server to drop (destroy) a database
@@ -352,7 +353,7 @@
mutable std::string error_message_; ///< MySQL++ specific error, if

- DBDriver* driver_;
+ std::tr1::shared_ptr<DBDriver> driver_;
bool copacetic_;

Dmitry <gz_>


Attached Files
file #12486:  main.cpp added by gz_ (298B - text/x-c++src)
file #12487:  patch.diff added by gz_ (527B - text/x-patch)
file #12488:  before.txt added by gz_ (4kB - text/plain)
file #12489:  after.txt added by gz_ (987B - text/plain)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -unavailable- added by bunkar (Posted a comment)
  • -unavailable- added by wyoung (Posted a comment)
  • -unavailable- added by gz_ (Submitted the item)
  • -unavailable- added by gz_

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.


    Error: not logged in



    Follow 8 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Thu Dec 12 15:19:36 2013wyoungStatusInvalid=>Fixed
    Sat Feb 19 09:25:38 2011gz_Attached File-=>Added main.cpp, #12486
      Attached File-=>Added patch.diff, #12487
      Attached File-=>Added before.txt, #12488
      Attached File-=>Added after.txt, #12489
    Fri Feb 18 23:42:51 2011wyoungStatusNone=>Invalid
    Fri Feb 18 21:46:54 2011gz_Carbon-Copy-=>Added -unavailable-
    Show feedback again

    Back to the top

    Powered by Savane 3.1-cleanup