Bug #35246 Consistent memory leak in debug when sending query from different thread
Submitted: 12 Mar 2008 12:18 Modified: 16 May 2008 16:12
Reporter: bob leponge Email Updates:
Status: No Feedback Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S6 (Debug Builds)
Version:Repository OS:Windows
Assigned to: CPU Architecture:Any
Tags: debug, memory leak, thread, tls

[12 Mar 2008 12:18] bob leponge
Description:
When calling mysql_real_query from different thread, with USE_TLS defined, and DEBUG defined, the client calls (inverted stack trace):
    \mysys\my_thr_init.c (170): my_thread_init
    \mysys\my_thr_init.c (246): _my_thread_var
    \dbug\dbug.c (352): code_state
    \dbug\dbug.c (717): _db_enter_

Which allocate a small (150 bytes) buffer stored on the TLS, but on the heap, which is never freed.
(The revelant line is :
    if (!(tmp= (struct st_my_thread_var *) calloc(1, sizeof(*tmp))))
in my_thr_init.c)
The library doesn't own the thread, so it can't monitor when it's freed, so it leaks the memory when the thread is destructed.

However, the main thread's TLS version is cleaned in the mysql_close part, but any other thread leaks the sizeof(*tmp) version (plus the DbgMalloc part of the code_state function).

The thread is 

How to repeat:
Compile with -DUSE_TLS, -DSAFE_MUTEX, -DSAFE_MALLOC, -DDEBUG
Create a main thread, perform your usual mysql_real_query, and free your results.
Check the memory leaks with your favorite tool, it should be clean.
Then allocate a thread, call the mysql_real_query in the thread (and free your results).
You'll see the memory leak then.

Suggested fix:
Allocate the tmp buffer on the TLS(and not on the heap) so it's automatically cleaned when the thread is closed.
[12 Mar 2008 12:58] Sveta Smirnova
Thank you for the report.

Please indicate exact version of MySQL you use and provide compete test case.
[12 Apr 2008 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".
[16 Apr 2008 16:12] Susanne Ebrecht
Bob, 

we need to know in which versions you found this code.
[16 May 2008 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".