Bug #47841 Memory leak in libmysql.dll
Submitted: 5 Oct 2009 22:16 Modified: 18 Jul 2010 10:04
Reporter: R H Email Updates:
Status: No Feedback Impact on me:
Category:MySQL Server: C API (client library) Severity:S2 (Serious)
Version:5.1.32 OS:Microsoft Windows (XP)
Assigned to: Assigned Account CPU Architecture:Any
Tags: client, Leak, libmysql, libmySQL.dll, library, Memory

[5 Oct 2009 22:16] R H
This is related to bug http://bugs.mysql.com/bug.php?id=45002


I have the a problem with a memory leak in the client library.
I have today downloaded the latest version of MySQL for windows xp 5.1.32, and I use the LibMySQL.dll from this installation. I am running applications on Delphi 7 and on Lazarus/FPC. I have a memory leak in both environments.

My problem is, when I call MySQL_Free_Result, sometimes the memory is deallocated and sometimes not. I can see this by debugging through my application and at the same time looking at memory consumption (private bytes and working set) in Process Viewer (http://www.teamcti.com/pview/prcview.htm)

I can also see that whenever MySQL_Free_Result fails to free the memory, subsequent repeated calls to MySQL_Query will eventually free the memory. But these calls must be done on same connection and from same thread. This means that memory continues to grow in multithreaded application, and at some point overgrow system resources.

Is there anyway arround this ? 

Kind regards

How to repeat:
1. Use Delphi 7 and get the MySQL.pas (translation of the c headers) from
2. Now startup loading and initializing the library etc
3. call MySQL_Query with a select sentence (something that returns large result so you can see impact on system memory)
4. call MySQL_Store_Result
5. make repeated calls to MySQL_Fetch_Row, until you get all rows
6. call MySQL_Free_result

Repeat the above step2 3-6 several times, and you will see that sometimes the memory is
not freed!!
[5 Oct 2009 22:23] Miguel Solorzano
Thank you for the bug report.

Step 1 from How to repeat:

Not Found

The requested URL /mysql.html.' was not found on this server.
[6 Oct 2009 5:16] R H

minor mistake 

[6 Oct 2009 18:33] R H
sorry for the typo. Real address is above
[7 Oct 2009 7:49] Tonci Grgin
Miguel, I do not think this is Delphi or mysql.pas related at all... It's just repeated calls to fetch and should be checked in plain C. Probably duplicate of that bug verified by Sveta.
[7 Oct 2009 18:07] R H
I agree that it is probably neither Delphi or MySQL.pas related. The is still there using Lazarus/FPC and MySQL50dyn.pas, as described earlier.

I am really hoping that there is a way to work around this. It seems like a very fundamental part of the API that is broken. How does everyone else use MySQL then ?
[11 Oct 2009 8:37] Sveta Smirnova
Thank you for the report.

If you think this is same as bug #45002 why do you open new one? What is the difference?
[11 Oct 2009 21:53] R H
Well actually I have never seen valgrind output before and I dont read C code well. Forgive me my ignorance in these fields

I thought the bug might be related, but I am not sure. I have located the bug to the MySQL_Free_Result function, which sometimes does free allocated memory by MySQL_Store_Result, and sometimes not. I thought this info might help fixing the bug.
[18 Jun 2010 10:04] Sveta Smirnova
Thank you for the feedback.

I can not repeat described behavior with C test case. Please send us complete Delphi project so we can try it.
[18 Jul 2010 23:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".