Reputation: 11768
I am writing a simple C program to request some data from a mysql db the function does not report a memory leak when used in a single thread application, but while in a thread it does.
The function is this I have stripped the unnecesary code
BOOL getip(char * ip,int * port,BOOL serie)
{
MYSQL * conn,mysql;
MYSQL_RES * res;
MYSQL_ROW row;
char * server = "someip";
char * user = "root";
char * pass = "root";
char * db = "testdb";
char * query = NULL;
mysql_init(&mysql);
mysql_close(&mysql);
}
> ==30295== HEAP SUMMARY:
> ==30295== in use at exit: 73,872 bytes in 21 blocks
> ==30295== total heap usage: 85 allocs, 64 frees, 119,908 bytes allocated
> ==30295==
> ==30295== 128 bytes in 1 blocks are definitely lost in loss record 2 of 5
> ==30295== at 0x402A629: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
> ==30295== by 0x408FB63: my_thread_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
> ==30295== by 0x408FE43: my_thread_global_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
> ==30295== by 0x408E2D7: my_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
> ==30295== by 0x406B3FA: mysql_server_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
> ==30295== by 0x406CB28: mysql_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
> ==30295== by 0x8048DD1: getip (database.c:22)
> ==30295== by 0x80492DD: sockit (listener.c:50)
> ==30295== by 0x4378D4B: start_thread (pthread_create.c:308)
> ==30295== by 0x447BD3D: clone (clone.S:130)
> ==30295==
> ==30295== LEAK SUMMARY:
> ==30295== definitely lost: 128 bytes in 1 blocks
> ==30295== indirectly lost: 0 bytes in 0 blocks
> ==30295== possibly lost: 0 bytes in 0 blocks
> ==30295== still reachable: 73,744 bytes in 20 blocks
> ==30295== suppressed: 0 bytes in 0 blocks
> ==30295== Reachable blocks (those to which a pointer was found) are not shown.
> ==30295== To see them, rerun with: --leak-check=full --show-reachable=yes
every time the function executes the leak increases so it got to be an error
What am I doing wrong ?
Upvotes: 3
Views: 673
Reputation: 41017
Does mysql_library_end();
at the very end helps?
Test this:
int main(void)
{
MYSQL *mysql;
mysql_init(mysql);
mysql_close(mysql);
mysql_library_end();
return 0;
}
Upvotes: 3