[lazarus] mysql unit crashing (somewhat solved?)

Tony Maro TonyM at nlisc.com
Sat Sep 7 13:00:22 EDT 2002


For those of you working with mysql, I've figured out that it doesn't appear to allocate memory correctly for the TMYSQL record unless it's a global variable.  If you place the TMYSQL record inside an object such as:

....snip....
type
    { TMysqlLink }
    TMysqlLink = class( TComponent )
    private
           FDatabase: String;
           Fsock : PMYSQL;
           Fqmysql : TMYSQL;
....snip....

It's got a good chance of crashing when you access Fqmysql such as through a mysql_connect function.

My solution was to just create a pointer to a TMYSQL and then manually allocate the memory for it, freeing that memory when my component is destroyed.  No more crashes.

....snip....
type
    { TMysqlLink }
    TMysqlLink = class( TComponent )
    private
           FDatabase: String;
           Fsock : PMYSQL;
           FPMysql: ^TMYSQL;
....snip....

// then later I do...
GetMem(FPMysql, sizeof(TMYSQL));

// and use the following to connect...
Fsock := mysql_connect(FPMysql,PChar(FServer),PChar(FUser),PChar(FPass));

....snip....

If you're only using one possible ever connection, it works if you place the TMYSQL in a global variable.  If you want the opportunity to connect more than once, simultaneously, the above should work.

Could be just a fluke, but it appears to work every time now.

-Tony


<<winmail.dat>>



More information about the Lazarus mailing list