Subject Re: [IBO] To use TIB_ as parameter DLL, need to use ShareMem or SimpleShareMem in uses clause?
Author hamacker
Not my DLL in threads, but when I use Datasnap (clientdataset), I see separate threads on my debbuging, some goals are finish but result data still in process that I can view by refreshing.

I will looking for a method using bpl as plugin, when I found a safe mode to load/unload .bpl I will replace my dll´s plugins.


2015-02-04 12:34 GMT-02:00 Geoff Worboys geoff@... [IBObjects] <IBObjects@yahoogroups.com>:
 

hamacker sirhamacker@... [IBObjects] wrote:
> Yes, it´s the same when I use ClientDataset, that always in
> threads. Load and Unload plugin too fast it´s a risk.

"always in threads"? You're not trying to load and unload the
libraries in separate threads are you? I think that would be
a bad idea with any libraries using VCL components. Your code:

> Note my remark:
>   (...)
>   LibHandle := SafeLoadLibrary(sArquivo);
>   try
>     if LibHandle = 0 then
>     begin
>       Application.MessageBox(sArquivo,Pchar('Erro ao carregar ['+sModulo+'] :'));
>       Exit;
>     end;
>     @ModuloInfo:=GetProcAddress(LibHandle, 'ModuloInfo');
>     sResultado:=String(ModuloInfo);
>   finally
>     bOK:=false;
>     while not bOK do
>     begin
>       bOK:=FreeLibrary(LibHandle);
>       //Sleep(500);  <--- for debug
>       Application.ProcessMessages;
>     end;

is definitely not for a separate thread (not with it's
Application.xxxx calls) so maybe that's not what you meant.

The point is to NOT release the library until you know it is
safe to do so, rather than repeatedly trying. It seems very
strange to load a library just to call one function and then
immediately release it again - it's hard to imagine what you
must be doing in that the function to make it worth this
effort, or why it is so urgent to release the library.

> In the past I used Sleep(x) to workaround AVs with timer or
> thread troubles, now I doing using 'while not FreeLibrary'
> and I dont need Sleep(x) anymore. But sometimes when AVs
> appear like a ghost :(,  I debug unremarking Sleed(x) to see
> if my unload method still effective. One warning in this
> process is DON´T LOAD DLL RECURSIVELY.

Sleep will only help if you are waiting on other threads, and
there are better ways of dealing with the required thread
synchronisation - you certainly shouldn't need to hold up the
entire main thread in the manner of your code above, especially
if the function is long running.

Without knowing more it's hard to guess what you should be
doing, but I suspect you need to redesign how you are managing
this if you want some reliability. For example: load the
library on the first request, but through reference counters
or similar only try to unload (in OnIdle or similar) some time
after it has last been used (if necessary, some time after the
last thread that needed it has finished with it).

> Some guys recommend to use .bpl insted dll, but I don´t know
> how.

That's why I mentioned the LoadPackage and UnloadPackage
functions. You can look these up in the help. (Dynamically
loading packages can get complicated to handle when you start
talking about registering new classes and so on, but much less
so than trying to do the same thing by DLL.)

With DLLs you are going to have to be very careful with your
linking - or you will get duplicated globals between that and
your application, and that could get really confusing. This
is one of the reasons why BPLs are a much better choice when
dealing with Delphi units.

(We are getting off-topic for the IBObjects forum, because
the problems you are facing have nothing to do with IBO. If
you aren't sure how to do this stuff you probably should be
asking for help from a Delphi forum.)

--
Geoff Worboys
Telesis Computing Pty Ltd