Recent

Author Topic: Call to external library 64bit vs 32bit  (Read 3919 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 19132
  • Glad to be alive.
Re: Call to external library 64bit vs 32bit
« Reply #15 on: March 20, 2022, 05:52:00 pm »
The sharemem unit? (Obviously) Note this only works for Pascal on both sides, lib and exe. Anything other and the best thing you can do is use PChars.
objects are fine constructs. You can even initialize them with constructors.

440bx

  • Hero Member
  • *****
  • Posts: 6473
Re: Call to external library 64bit vs 32bit
« Reply #16 on: March 20, 2022, 07:47:42 pm »
The memory managers have the same code (albeit located in both the library and the main program), but their state is different between library and main program and that is the issue.
That's the point I was trying to make which apparently I didn't make very well.  With managed types the problem is exacerbated by the fact that either memory manager may decide to "manage" the type whenever it is given access to it causing the other memory manager to have state information about that variable which is then out of synch.

With managed types it's very easy to end up in the situation "too many cooks in the kitchen". 


FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6393
  • Compiler Developer
Re: Call to external library 64bit vs 32bit
« Reply #17 on: March 21, 2022, 11:14:22 am »
OK clear and understandable statement. I have just one question how would you go about to share the memory manager? Lets start when both are written in FPC although a c/c++ dll might interest me too in the future.

Either don't use managed types across module boundaries and provide explicit FreeXYZ functions from your library so that memory is freed using the correct memory manager, use a shared memory manager like sharemem as suggested by Thaddy or pass the memory manager of the application to the library (though then you need to make sure that both are using the same version as the functions of the memory manager might differ between versions).

The memory managers have the same code (albeit located in both the library and the main program), but their state is different between library and main program and that is the issue.
That's the point I was trying to make which apparently I didn't make very well.  With managed types the problem is exacerbated by the fact that either memory manager may decide to "manage" the type whenever it is given access to it causing the other memory manager to have state information about that variable which is then out of synch.

With managed types it's very easy to end up in the situation "too many cooks in the kitchen". 

More or less...

Чебурашка

  • Hero Member
  • *****
  • Posts: 593
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Call to external library 64bit vs 32bit
« Reply #18 on: March 22, 2022, 03:05:47 pm »
switched to PChar as suggested
No problem with that but, it should work with "string" too.

Few more bits: I endeed up calling the library functions from a exe compiled with Kylix 32 bit and as said by others, it didn't work using string.
In fact such Kylix installation was configured to use by string as an alias for ShortString, therefore what in lazarus is a string (AnsiString) was no way compatible. Converting back and forth on two sides to PChar was the solution, as stated.

If both system were configured to use AnsiStrings, probably I would have seen it working, but only by coincidence if managed structured happened to be the same.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Call to external library 64bit vs 32bit
« Reply #19 on: March 25, 2022, 11:26:06 pm »
OK clear and understandable statement. I have just one question how would you go about to share the memory manager? Lets start when both are written in FPC although a c/c++ dll might interest me too in the future.

Either don't use managed types across module boundaries and provide explicit FreeXYZ functions from your library so that memory is freed using the correct memory manager, use a shared memory manager like sharemem as suggested by Thaddy or pass the memory manager of the application to the library (though then you need to make sure that both are using the same version as the functions of the memory manager might differ between versions).


Thank you, I'll take a closer look at sharemem and see how it works.

 

TinyPortal © 2005-2018