Using FastMM4, how do I log the missing line?

With FastMM4 it is easy to register a missing pointer, but no string leaked. Apparently, the operator @

applied to a string doesn't actually give us the whole string, nor does PChar(string)

; What can I use to log the missing line well?

So far I've found this to work:

FastMM4.RegisterExpectedMemoryLeak(Pointer(NativeInt(PChar(StringVariable))-12));

      

But it relies on the magic number 12

and is version dependent and the code doesn't really express what's going on. I hope there is an RTL function out there that takes a string and returns a pointer to the "base" of the string, or some method FastMM4

I missed.

I can put together this ugly expression beast in a routine like this, but I still find it a hack:

procedure RegisterExpectedStringLeak(const s:string);
begin
  {$IFDEF VER210}
  FastMM4.RegisterExpectedMemoryLeak(Pointer(NativeInt(PChar(s))-12));
  {$ELSE}
  {$MESSAGE Fatal 'This only works on Delphi 2010'}
  {$ENDIF}
end;

      


It shouldn't matter to the question. This is why I am leaking lines:

I use a caching mechanism to store specific chunks of data for the lifetime of the application. I do not intend to free these objects, because I really need them for the entire life of the application, and with proper completion, only the waist time when the application is shut down. These objects contain some string fields, so it is obvious that these strings are "leaked".

+1


source to share


1 answer


The closest I can think of would be:

function RegisterExpectedStringLeak(const S: string): Boolean;
  type
    {Have to redeclare StrRec here, because it is not in the interface section of system.pas}
    PStrRec = ^StrRec;
    StrRec = packed record
      {$ifdef CONDITIONALEXPRESSIONS}
      {$if RTLVersion >= 20}
      codePage: Word;
      elemSize: Word;
      {$ifend}
      {$endif}
      refCnt: Longint;
      length: Longint;
    end;
begin
  Result := RegisterExpectedMemoryLeak(Pointer(NativeInt(PChar(S)) - SizeOf(StrRec)));
end;

      



The re-declaration is StrRec

copied from Delphi XE getmem.inc

(FastMM4) - I only added {$ifdef CONDITIONALEXPRESSIONS}

. I think it should be backward compatible with older Delphi versions.

+2


source







All Articles