Subject Re: [IBO] To use TIB_ as parameter DLL, need to use ShareMem or SimpleShareMem in uses clause?
Author hamacker
The reason for my question is that some times (its random), my application data crash with access viollation, but not inside DLL but outside after used, when my method was released by FreeLibrary. And it´s not clear if it´s my DLL, ´cause the error appear in any screen, but I investigate, if DLL not used, not crashed does not matter screen.

 I create a method to use DLL as plugin to load dynamic and release after, I do an unit test and everthing is OK before exchange to production enviroment, but access viollation it´s evil surprise for me, so I will change TIB_Conn parameter to a colletion params WideString to create TIB_Connection inside DLL and not use by parameter.

Thanks a lot.

2015-02-03 11:54 GMT-02:00 Geoff Worboys geoff@... [IBObjects] <>:

hamacker sirhamacker@... [IBObjects] wrote:
> Hi everybody,

> Who knows if is necessary to declare  ShareMem or
> SimpleShareMem in uses clause when I need to pass
> IB_Connection by parameter as :

> function ModuloRodar(MyConn:TIB_Connection;sParam:WideString):
> WideString; stdcall;

It would seem unusual to pass Delphi component references
between a DLL and an application (in either direction). The
normal method would be to use runtime packages - in which case
ShareMem is not relevant. But if you were using a DLL, for
whatever reason, then it might be expected that the DLL would
have been built with the same Delphi compiler, in which case
you are probably already using the same memory memory manager
so ShareMem shouldn't matter. If the DLL is not from Delphi it
would be more common to pass the Firebird database handle
across to the DLL rather than the full connection component.

But what is usual or unusual aside, it's all about memory
management. The reason why can be is necessary for long string
and dynamic arrays is that their memory management is automatic
so you don't have much control over when they might reallocate
or free memory. (Although, in a typical DLL scenario it is
feasible to send pointers into arrays/strings across the DLL
link without needing ShareMem as long as you know there will
be no reallocations while it is in use across the interface and
you know the interface won't retain pointers into the memory.
This sort of thing happens all the time in IBO as it interacts
with the fbclient.dll.)

If your DLL is creating any Delphi components for use by an
application, or the application is creating components for use
by the DLL, then ShareMem will be necessary unless that shared
use avoids anything that might cause memory (re)allocations on
either side while the object is in use (and knowing that means
being familiar with the source on both sides).

And regardless of all that, in the example above you pass and
return WideString, so ShareMem would be necessary.

Geoff Worboys
Telesis Computing Pty Ltd