Sharing Memory

From RAD Studio
Jump to: navigation, search

Go Up to Managing Memory Index


This section describes how to share memory using the Memory Manager.

On Win32 and Win64, if a DLL exports routines that pass long strings or dynamic arrays as parameters or function results (whether directly or nested in records or objects), then the DLL and its client applications (or DLLs) must all share the same memory manager. The same is true if one application or DLL allocates memory with System.New or System.GetMem which is deallocated by a call to System.Dispose or System.FreeMem in another module. There are two mutually exclusive methods through which the Memory Manager can be shared between an application and its libraries: ShareMem and SimpleShareMem.

On Posix, the application and shared-library already share to use System library including malloc, free and realloc.

Note: When a DLL is statically linked to an application, the DLL is initialized before the application. The application will use the memory manager of the DLL if the SimpleShareMem sharing method is used in both. Only the module that is sharing its memory manager can change memory manager settings and retrieve memory manager statistics. Changing settings in any of the other modules will have no effect, since their memory managers are not used. It is possible, but rarely needed, to control the memory manager sharing mechanism manually.

To use ShareMem

  1. List ShareMem as the first unit in the program and library uses clause. Your modules will become dependant on the external BORLNDMM.DLL library, allowing them to share dynamically allocated memory.
  2. Deploy BORLNDMM.DLL with your application or DLL that uses ShareMem. When an application or DLL uses ShareMem, its memory manager is replaced by the memory manager in BORLNDMM.DLL.

To use SimpleShareMem

  1. List SimpleShareMem as the first unit in the program and library uses clause in each of your modules. The module that is initialized first will be the module that will share its memory manager. All modules initialized after that will use the memory manager of the first module.
  2. The module that is initialized first will be the module that will share its memory manager. All modules initialized after that will use the memory manager of the first module.

See Also