[DUG] memory leak tools

Jianming Lin (FMI) jianmingl at fmi.co.nz
Mon Aug 4 11:18:19 NZST 2008


 

It doesn't work in Delphi 2006, not fully modified to run in D2006.
Author didn't even test by compiling it in delphi2006.

 

For example:

 

function CallerIsNewInstance: boolean; 

asm

...

     mov eax, [EBP + 12]          -> it shoule be mov ax, [ebp + 8]

     sub eax, 15

    cmp eax, AddressOfNewInstance  //const AddressOfNewInstance: pointer
= @TObject.NewInstance;

...

 

end;

 

Reason is as follows:

Go to the definition of TObject.NewInstance in system unit:

 

class function TObject.NewInstance: TObject;

begin

  Result := InitInstance(_GetMem(InstanceSize));

end;

 

It calls _GetMem:

 

function _GetMem(Size: Integer): Pointer;  // stack pointer -4

begin

...

asm

        TEST    EAX,EAX

        JLE     @@negativeorzerosize

        CALL    MemoryManager.GetMem //when getting in, stack pointer -4
again 

        TEST    EAX,EAX

        JZ      @@getmemerror

        DB      $F3

        RET

...

end;

 

In GetMem, it calls MemoryManager.GetMem, which is redirected to
LeakTrackingGetMem in MemCheck, 

and stack pointer is stored in cpu register ebp;

 

So when the program runs into CallerIsNewInstance, stack pointer -4 by
two times: 

  run into _GetMem decrease by -4, 

  run into LeakTrackingGetMem, decrease by -4 again.

 

the return address of GetMem is then stored in [ebp + 8] not in [ebp +
12].

 

Similarly, you can verify that:

1)      in ltfm_CallerOfFreeInstance:  mov eax, [ebp + 28]  should be :
mov eax, [ebp + 16]

2)      in ltfm_CallerOf_FreeMem  :  mov eax, [ebp + 12]  should be :
mov eax, [ebp + 8]

3)      in ltgmCallerOfGetMemIsTObjectCreate: mov eax, [ebp + 36]
should be : mov eax, [ebp + 32]

4)      in ltgmCallerOfTObjectCreate : mov eax, [ebp + 56]  should be :
mov eax, [ebp + 52]

5)      in ltgmCallerIsNewAnsiString  : mov eax, [ebp + 12]  should be :
mov eax, [ebp + 8]

 

And Delphi2006 doesn't support the syntax unitname.unitname anymore.

e.g. 

procedure ChangeFinalizationsOrder;

...

begin

  ...

  if CurrentUnitInfo.Init = @Variants.Variants then

...

end

 

Compiler will complaint : E2003 Undeclared identifier: 'Variants'

 

To get the address of a unit definition, the following trick will do
job:

 

procedure ChangeFinalizationsOrder;

...

var UnitPtr : Pointer;

...

begin

    asm

      push eax;

      mov eax, offset Variants.Variants;

      mov UnitPtr, eax;

      pop eax;

    end; 

...

   if CurrentUnitInfo.Init = UnitPtr then //@Variants.Variants then

 

....

 

 

One more thing :

 

As you change the code of Memcheck.pas,  entry point calculation may
wrong : 

 

Const DummyToFinalizationOffset = {$IFOPT I+}356{$ELSE}352{$ENDIF};

begin

   ...

   If CurrentUnitInfo.Init = Pointer(PChar(@Dummy) +
DummyToFinalizationOffset) then ...

end

 

you can get correct entry point of a unit by putting the following asm
code at the very beginning of initialization part:

 

initialization

asm

  push eax;

  mov memchkPtr, esi;             //esi stores the the offset address of
memcheck.memcheck.

  pop eax;

end;

 

 

memchkPtr is defined at:

 

implementation

 

uses Windows,  classes, Math, ...

 

var memchkPtr : Pointer;          

 

type TKindOfMemory = (MClass, MUser, MReallocedUser);

...

 

That's all you need to do to make D2006 happy with memCheck.pas. 

________________________________

From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz]
On Behalf Of Leigh Wanstead
Sent: Wednesday, 30 July 2008 5:29 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] memory leak tools

 

http://v.mahon.free.fr/pro/freeware/memcheck/

 

Have a nice day

 

Regards

Leigh

www.smootharm.com

	-----Original Message-----
	From: delphi-bounces at delphi.org.nz
[mailto:delphi-bounces at delphi.org.nz]On Behalf Of Vikas...
	Sent: Wednesday, 30 July 2008 5:10 p.m.
	To: NZ Borland Developers Group - Delphi List
	Subject: [DUG] memory leak tools

	Hi,

	 

	Is there any open source or free memory leakage tool available
to track memory leakage in delphi application.

	 

	 

	Regards

	Vikas
	

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20080804/baa78af9/attachment-0001.html 


More information about the Delphi mailing list