Page 1 of 1

Dynamic library loading

Posted: 27.06.2006, 12:37
by aebi
Hi,
when loading a library using "LoadLibrary", which itself uses ZConnection an such things, I have a problem with connections in the main programm (AV). My Debuger (event protocol) tells me, that - when starting the main programm - the libpq.dll is loaded (I'm using Delphi 7 and PostgreSQL); when my own DLL is loaded everything is ok, but when I call "FreeLibrary" the libpq.dll is also unloaded ! - and of course - the main programm crashes (I think the TPlainLoader has still all the proc-adresses).
Any ideas ?

Greetings Andreas

Posted: 27.06.2006, 13:40
by aebi
Hi again - we've got him !
I found a reason for this behavior : it's "GetModuleHandle" in ZPlainLoader; this call *doesn't* increment the referencecounter for the modul (libpd.dll) so the next call to FreeLibrary unloads it (the counter is still 1 from the main programm). I don't know, why this call is made, without it the (normal) LoadLibrary is called (which increments the referencecounter) - and everything works fine !

Greetings Andreas :-)

Posted: 27.06.2006, 14:27
by mdaems
Hi,

I'm not a specialist in loading DLLs, but as I see it this "GetModuleHandle" is use to avoid that a database DLL is loaded twice, eg when one uses a connection with protocol mysql5 and another with protocol mysql the same dll will be used. Is it possible to load the same DLL twice from the same program using LoadLibrary? Would that cause the library to be loaded twice in memory or would it be reused?
The more : is this behaviour identical if FPC on Windows and FPC on Linux?

Who can give some more insights on this?

Mark

Posted: 27.06.2006, 15:29
by aebi
Hi,

IMHO a DLL is only loaded once (as the SDK-Help to LoadLibrary says); I've found a ifdef-ed Funktion "GetModuleHandle" in ZCompatibility which returns always 0 (zero), so I think on non-windows-systems *always* the LoadLibrary (or dlopen because this function is alsoifdef-ed in ZCompatibility) is called. BTW : the IFDEF is FPC.

Andreas

Posted: 07.07.2006, 09:52
by sgmeiner
100% affirmative. Replacing the line with GetModuleHandle fixed my problem (after searching my butt off all the week - sorry for my language). Thanks aebi!

I would be delighted if some code using LoadLibrary would show up in one of the SVN-Packages ;-)

Kind regards and keep up the good work!

Sebastian

Posted: 07.07.2006, 12:16
by mdaems
Hi Sebastian,

I'll comment out this piece of code in the SVN testing branch version. I can't do it right now, but you can expect it within a few days.

Mark

Posted: 07.07.2006, 21:29
by mdaems
Hi this should be commented out now in SVN testing branch. I'm packaging... takes some time.You can see it soon on http://users.telenet.be/mdaems

Mark