[fpc-devel] Mem-leak, where?

Michael Van Canneyt michael at freepascal.org
Wed Sep 26 09:22:33 CEST 2007



On Wed, 26 Sep 2007, Joost van der Sluis wrote:

> Op woensdag 26-09-2007 om 02:29 uur [tijdzone +0400], schreef Sergei
> Gorelkin:
> > Joost van der Sluis wrote:
> > > ----
> > > program testbug9751;
> > > 
> > > {$mode objfpc}{$H+}
> > > 
> > > uses
> > >   Classes, SysUtils, mysql41dyn;
> > > 
> > > var hmysql : PMYSQL;
> > > 
> > > begin
> > > //  InitialiseMysql; <-- remove the comment and the leakage is gone
> > >   repeat
> > >   InitialiseMysql;
> > >   hmysql := nil;
> > >   hmysql := mysql_init(hmysql);
> > > // Comment the next line and the leakage is gone
> > >   HMySQL:=mysql_real_connect(HMySQL,PChar(''),PChar(''),Pchar(''),Nil,0,Nil,0);
> > >   mysql_close(hmysql);
> > >   ReleaseMysql;
> > >   until 1<0;
> > > end.
> > > ----
> > 
> > Looks like mysql_real_connect() overwrites hmysql variable? The value 
> > returned by mysql_init() is not passed to mysql_close(), and the library 
> > may not unload in this case.
> > If you comment mysql_real_connect(), handle returned by mysql_init() IS 
> > passed to mysql_close().
> 
> I also thought that. But that's not the case. From the
> mysql-documentation:
> (http://dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html)
> 
> ----
> Return Values 
> 
> A MYSQL* connection handle if the connection was successful, NULL if the
> connection was unsuccessful. For a successful connection, the return
> value is the same as the value of the first parameter. 
> ----
> 
> Also in the case of a succesfull connection, this code leaks...
> 
> I've rewritten the code, so that it's more clear what happens:
> -----
> program testbug9751;
> 
> {$mode objfpc}{$H+}
> 
> uses
>   Classes, SysUtils, mysql41dyn,dynlibs;
> 
> var hmysql : PMYSQL;
>     LH : TLibHandle;
> 
> begin
>   repeat // repeat here gives troubles
>   LH := loadlibrary(mysqllib);
>   pointer(mysql_init) := GetProcedureAddress(LH,'mysql_init');
>   pointer(mysql_real_connect) := GetProcedureAddress(LH,'mysql_real_connect');
>   pointer(mysql_close) := GetProcedureAddress(LH,'mysql_close');
> 
> // repeat; <- if the repeat is placed here, the problems are gone
>   hmysql := nil;
>   hmysql := mysql_init(hmysql);
> // Remove the next line and the problem is gone.
>   HMySQL:=mysql_real_connect(HMySQL,PChar('192.168.3.1'),PChar('root'),Pchar('rosivrepus'),Nil,0,Nil,0);
>   mysql_close(hmysql);
> 
> //  until 1<0; <- if the until is placed here, the problems are gone
>   UnloadLibrary(LH);
>   until 1<0; // until here gives troubles
> end.
> -----
> 
> so you can solve the leak by two ways:
> 1: remove the mysql_real_connect
> 2: move the repeat-until so that the 'initialisation' is outside the
> loop

That points to a memory leak in libmysqlclient itself, no ?
Maybe it is not meant to be loaded/unloaded ?

Michael.



More information about the fpc-devel mailing list