0

I have some Delphi 6 code that allocates memory using New and frees it using Dispose. I have stepped through the code and see that both New and Dispose actually do get called.

I am wondering why FastMM is reporting a memory leak, even though Dispose is called. Could it be related to the fact that the item being disposed is in the var section?

type TRunP = record
  channels      : Word;     (* channels for program       *)
  GCharH        : Word;        (* Character Height        *)
  GCharW        : Word;        (* Character Width         *)
... [snip]
end;
...
PRunPBuf = ^TRunP; //record pointer
...

var
  ptrRunPBuf: PRunPBuf;

...
New(ptrRunPBuf);   //here is the problem, FASTMM claims this was never disposed, but dispose was called without error.  
...
Dispose(ptrRunPBuf);   

Here is the information from FastMM.

--------------------------------2011/10/4 16:29:08--------------------------------
A memory block has been leaked. The size is: 276

This block was allocated by thread 0x1BBC, and the stack trace (return addresses) at the time was:
40002FCF [System][@GetMem]
6243EB [dir\mfs1.pas][MFshell1][TfrmM.FormCreate][1806]
400EDDA7 [Forms][TCustomForm.DoCreate]
400EDA1A [Forms][TCustomForm]
7E428EAB [Unknown function at DefWindowProcW]
7C90D98A [ZwQueryVirtualMemory]
7C80BA5D [VirtualQueryEx]
7C80BA86 [VirtualQuery]
1148FD9 [GetFrameBasedStackTrace]
114901C [GetFrameBasedStackTrace]
1149336 [GetRawStackTrace]

The block is currently used for an object of class: Unknown

The allocation number is: 62982

Current memory dump of 256 bytes starting at pointer address 7FF40410:
44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00
44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00
44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00
44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00
44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00
44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00
44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00
44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00 44 99 63 00
D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .
D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .
D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .
D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .
D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .
D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .
D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .
D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .  D  ™  c  .

--------------------------------2011/10/4 16:29:08--------------------------------
sse
  • 987
  • 1
  • 11
  • 30
  • Please can you show a complete program, preferably in a single .dpr file. – David Heffernan Oct 05 '11 at 14:31
  • 1
    Where is the Dispose() called? In the TfrmM.FormDestroy event? Are you sure you don't call New() twice? – Arnaud Bouchez Oct 05 '11 at 14:41
  • 1
    1) Make sure that FastMM is the first using clause in your project 2) Make sure that you assign to that variable only once. I suspect you call `new` twice and only release the last value in the var. – CodesInChaos Oct 05 '11 at 15:04
  • The code is huge, and spans multiple units. An actual example is not possible. @Arnaud, There are multiple calls to new, but they are balanced by calls to dispose. I suppose it is possible that some of the calls to dispose don't happen. I will dig deeper. The code base is huge, and the declaration of the TRecord is in a different unit. Ideally everything would be in the same unit, unfortunately it is not. Thank you! – sse Oct 05 '11 at 16:26
  • @Arnaudbouchez, yes dispose is called in formdestroy. – sse Oct 05 '11 at 16:32
  • In order to track New() "orphans" calls, you can use a counter which is incremented at each New() and decremented at each Dispose() - at the end of the application, you shall have this counter = 0. I hardly think of a FastMM4 issue here. – Arnaud Bouchez Oct 05 '11 at 16:56
  • If `FastMM4` is the first unit in the .dpr uses clause then it **will** be telling you the truth. – David Heffernan Oct 05 '11 at 17:54
  • @Arnaudbouchez, Could you post your response as an answer, so I can accept. Thank you. – sse Oct 07 '11 at 20:37

0 Answers0