[fpc-pascal] Strings and objects crossing dll boundaries

Marco van de Voort marcov at stack.nl
Thu Jun 14 12:15:00 CEST 2012

In our previous episode, kyan said:
> A couple of questions regarding handling of strings, dynamic arrays
> and objects -especially exceptions- when writing programs that use
> shared libraries (.dll, .so).
> In Delphi we have the option of either using packages to ensure that
> there is only one instance of the memory allocator or use ShareMem.pas
> that installs a memory allocator that can handle memory allocations
> across libraries. Given that in FPC/Lazarus there are no "packages" in
> the Delphi sense what is the equivalent of ShareMem.pas? Is it
> cmem.pp?
> Regarding objects, in Delphi an object cannot safely cross dll
> boundaries because separate class record(s) for it exist in each
> executable file. OK, actually it can if one is certain that the class
> records compiled in each executable are identical. But if an exception
> crosses dll boundaries then you are in trouble, because the operator
> "is" does not work due to the class pointer that points to another
> Exception class record than the one known by the handling executable.

(and AS as it implies IS). Yes, all true.

> The best way to solve this is to put the class Exception at least
> inside a package that all executables use so at least "is" will work
> for Exception. Is there a way to handle this problem in FPC that does
> not have packages?

The Delphi packages concept consists out two key factors:

- the model that any metadata and RTL/program state only exists once in the process
  (EXE+DLLs combined)
- the way to easily achieve this. Basically Delphi requires to arrange 
  units into packages in ways that observe the dependency rules for
  packages, and the rest is fairly automated.

FPC does definitely not support the latter.

The former however (manually arranging for this uniqueness of state and
metadata) might be possible under certain circumstances.  There are multiple
reports about that (see among others
http://bugs.freepascal.org/view.php?id=12492 ), but whatever information
there is, is fragmented in bits and pieces, and there are no real structure
howtos.  Most are on Linux also.

More information about the fpc-pascal mailing list