5 messages in com.mysql.lists.mysqlRe: Memory leaks using MySQL C Api
FromSent OnAttachments
John McCaskey16 Jan 2004 11:16 
Chris Nolan17 Jan 2004 02:38 
Aftab Jahan Subedar17 Jan 2004 12:44 
John McCaskey17 Jan 2004 23:31 
KKoTY19 Jan 2004 03:38 
Subject:Re: Memory leaks using MySQL C Api
From:KKoTY (kko@seznam.cz)
Date:01/19/2004 03:38:16 AM
List:com.mysql.lists.mysql

try this, i found this when solving similar problem.There is another method to make and free connection, here is just one change. The trick is that you dont call mysql_close( ) on &mysql_connection , but on return of mysql_real_connect(); Try it, i think that this solve your is problem.

static MYSQL myMYSQL ; static MYSQL *myData ;

mysql_init(&myMYSQL);

if ( myData=mysql_real_connect( &myMYSQL, "localhost", "puberusr", "puber1234", "pubernew", MYSQL_PORT, NULL, 0 ) ) { if ( mysql_select_db( myData, "pubernew" ) < 0 ) { DisplayException(mysql_error(&myMYSQL)); mysql_close( myData ) ; } } else { DisplayException(mysql_error(&myMYSQL)); mysql_close( myData ) ; }

Agreed, I am not calling mysql_store_result(). I attempted to add my_free() but the function does not seem to exist, it is also not listed in the API docs for the c api. As such it still seems that there should be no leak, but yet I do get one. Thanks for the idea anyway Chris, maybe you can clarify for us once more.

On Sat, 2004-01-17 at 12:44, Aftab Jahan Subedar wrote:

Hey wait a minute. Where did you get the my_free(), may be you are trying to say mysql_free(), but then that is used only if result set is used/called.

But the code does not show any result set call. ie. mysql_use_result() or mysql_store_result().

So, the question now, how come there is a leak here. I dont see any, does anyone see any?

Chris Nolan wrote:

Hi!

You're looking for the function my_free(). Enjoy!

Regards,

John McCaskey wrote:

I have the following code:

//try the mysql connection

mysql_init(&mysql_connection);

if(!mysql_real_connect(&mysql_connection, db_host, db_user, db_pass, db_db, 0, NULL, 0)) {

flockfile(stderr);

fprintf(stderr, "%s: Failed to connect to database: Error: %s\n", timestamp, mysql_error(&mysql_connection));

funlockfile(stderr);

mysql_close(&mysql_connection);

return(2);

}

mysql_close(&mysql_connection);

This code is creating a memory leak. Am I missing some cleanup calls? I'm under the impression all I should need to do is call mysql_close to clean up the connection? I'm testing this using mtrace, if I place a return directly above the code segment it reports no leaks, if I place it direcly below the fragment there are several variables reported as not being freed. Any ideas?

John A. McCaskey

Aftab Jahan Subedar CEO/Software Engineer Subedar Technologies Subedar Baag Bibir Bagicha #1 North Jatrabari Dhaka 1204 Bangladesh http://www.SubedarTechnologies.com http://www.DhakaStockExchangeGame.com/ http://www.CEOBangladesh.com/ http://www.NYSEGame.com tel://+88027519050 EMail://jah@bol-online.com - Directly to my notebook