The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,259 other subscribers

Delphi: migrating applications + DLLs that use ShareMem to using FastMM

Posted by jpluimers on 2021/04/22

Notes to myself:

I bumped into some legacy code with a windows process and DLLs both using ShareMem (now System.ShareMem) so that strings could be shared between the instances.

There were lots of memory leaks, so migrating to FastMM was important.

I followed these steps to get rid of ShareMem:

  1. Put FastMM4 at the top of the uses lists for both the application and DLL projects
  2. Remove ShareMem from these uses lists (in fact from any unit used)
  3. Follow the FAQ ensuring these defines are globally in all projects involved: ShareMM;ShareMMIfLibrary;AttemptToUseSharedMM in each project file or the below in a fork of the FastMM4 repository file FastMM4Options.inc
    {$define ShareMM}
    {$define ShareMMIfLibrary}
    {$define AttemptToUseSharedMM}
    
    • Q: How do I get my DLL and main application to share FastMM so I can safely pass long strings and dynamic arrays between them?
    • A: The easiest way is to define ShareMM, ShareMMIfLibrary and AttemptToUseSharedMM in FastMM4.pas and add FastMM4.pas to the top of the uses section of the .dpr for both the main application and the DLL.
  4. Resolve any error like [dcc32 Error] E2201 Need imported data reference ($G) to access 'IsMultiThread' from unit 'FastMM4': in projects that depend on run-time packages. Luckily, how to do that is in the FAQ too:
    • Q: I get the following error when I try to use FastMM with an application compiled to use packages: “[Error] Need imported data reference ($G) to access ‘IsMultiThread‘ from unit ‘FastMM4‘”. How do I get it to work?
    • A: Enable the “UseRuntimePackages” option in FastMM4Options.inc.

Related:

Note:

  • I did not use SimpleShareMem (now System.SimpleShareMem) as the source of it did not tell me anything about FastMM4 compatibility.
  • A long time ago, FastMM changed the EnableBackwardCompatibleMMSharing from the old EnableSharingWithDefaultMM conditional define.

–jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.