FastMM: how can I RegisterExpectedMemoryLeak for all unicode or ansi strings?

174 Views Asked by At

There are three functions to register an expected memory leak in fastmm:

function FastMM_RegisterExpectedMemoryLeak(ALeakedPointer: Pointer): Boolean; overload;
function FastMM_RegisterExpectedMemoryLeak(ALeakedObjectClass: TClass; ACount: Integer = 1): Boolean; overload;
function FastMM_RegisterExpectedMemoryLeak(ALeakedBlockSize: NativeInt; ACount: Integer = 1): Boolean; overload;

but they are no applicable for string types. Any idea ?

1

There are 1 best solutions below

2
Serhii Kheilyk On

You can register these leaks as generic leaks, using function FastMM_RegisterExpectedMemoryLeak(ALeakedBlockSize: NativeInt; ACount: Integer = 1): Boolean; overload;

Here is an example:

program StringLeakDemo;

{$APPTYPE CONSOLE}

uses
  FastMM5,
  System.SysUtils;

const Leakee = 'string which leaks';
var Z: ^String;

begin
  FastMM_MessageBoxEvents := [Low(TFastMM_MemoryManagerEventType) .. High(TFastMM_MemoryManagerEventType)];
  FastMM_RegisterExpectedMemoryLeak(32 + Length(Leakee), 1);

  GetMem(Z, SizeOf(string));
  Z^ := Leakee;
  FreeMem(Z);
end.