Dynamic library loading

Forum related to version 6.5.1 (alpha) and 6.6.x (beta) of ZeosLib's DBOs

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
User avatar
aebi
Fresh Boarder
Fresh Boarder
Posts: 24
Joined: 02.05.2006, 10:26

Dynamic library loading

Post 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
User avatar
aebi
Fresh Boarder
Fresh Boarder
Posts: 24
Joined: 02.05.2006, 10:26

Post 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 :-)
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post 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
User avatar
aebi
Fresh Boarder
Fresh Boarder
Posts: 24
Joined: 02.05.2006, 10:26

Post 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
sgmeiner
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 25.10.2005, 14:39

Post 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
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post 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
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post 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
Post Reply