在 Delphi 中新建一个 DLL 工程时,在 dpr 中有这样的注释:
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
Delphi 要求在 DLL 和 EXE 之间导出 string、动态数组等自管理生存周期的对象时,将 ShareMem 放在 dpr 的 uses 列表第一项,以通过 BORLNDMM.DLL 为 DLL 和 EXE 提供共享的内存管理器。
如果不想用 ShareMem,就不要导出字符串和动态数组,而用 PChar、GetMem 等手工维护内存生命周期的指针来代替。
另外一个替代的方法是使用第三方的内存管理器 FastMM:
http://sourceforge.net/projects/fastmm/