Board index » delphi » Losing memory, is it a bug or am I doing this wrong

Losing memory, is it a bug or am I doing this wrong

I have to deliver my program and thought it might be wise to check and
see if I have any memory leaks.  I noticed it is but I'm pretty sure
it's not me the problem, I'm pretty neat about my pointers and always
try and make sure I clean after myself :).

If I'm doing this correctly can somebody else give me their results :

1) Create a new project.
2) Add Form2 : TForm; to the Var declaration
3) Add a button to the main form.
4) On the onclick event of the button add the following code

procedure TForm1.Button1Click(Sender: TObject);
Var
  MyHeap : THeapStatus;
  intI1, intI2 : Integer;

begin
  MyHeap := GetHeapStatus;
  intI1 := MyHeap.TotalAllocated;
  Application.CreateForm(TForm,Form2);
  Form2.ShowModal;
  Form2.Free;
  Form2 := nil;
  MyHeap := GetHeapStatus;
  intI2 := MyHeap.TotalAllocated;
  MessageDlg('Mem 1 : ' + IntToStr(intI1) + ' Mem 2 : ' +
IntToStr(intI2),mtInformation,[mbOk],0);
end;

I get six{*word*249} bytes lost the first time this is executed and 0 loss for
any other times it is executed.  I have absolutely no idea why though.
Unless MyHeap has nothing really to do with allocated memory and is
reporting god knows what.

Any feedback on this would be greatly appreciated.

--
Lior Amar
Computer Consultant
SCii Technologies
lior.a...@sncf.fr

 

Re:Losing memory, is it a bug or am I doing this wrong


In article <359B7DC3.C09EF...@aigf.sncf.fr>, Lior Amar
<lior.a...@aigf.sncf.fr> writes

Quote
>I have to deliver my program and thought it might be wise to check and
>see if I have any memory leaks.  I noticed it is but I'm pretty sure
>it's not me the problem, I'm pretty neat about my pointers and always
>try and make sure I clean after myself :).

[code snipped]

I haven't got time to try your code, but I have a similar memory leak in
my application - I call RegisterClass, and the first time I get a memory
leak of the same size as yours.

The culprit could be Application.CreateForm - this doesn't call
RegisterClass, but it does call NewInstance, which *might* call
RegisterClass. This is as far as I can diagnose the problem since I
don't understand all of that assembler in the system unit.

Anyway, if this *is* the problem, there's not a lot we can do about
it!
--
Jeremy Collins
Kansai Business Systems

(return address not altered 'coz I get spammed *whatever* I do!)

Re:Losing memory, is it a bug or am I doing this wrong


Quote
> I have to deliver my program and thought it might be wise to check and
> see if I have any memory leaks.  I noticed it is but I'm pretty sure
> it's not me the problem, I'm pretty neat about my pointers and always
> try and make sure I clean after myself :).
> Any feedback on this would be greatly appreciated.

Ok guys, solved the mistery of the disappearing bytes...  Seems like
when you launch an app it doesn't immediately create all the objects it
uses but as soon as you start creating different forms with different
objects that are used elsewhere it creates the object.  Strangely enough
releasing the parent object does not release all the associated
objects.  This is about as good as an explanation as I can get even
though I myself am not 100% convinced by this.  Found it in an old
WINDOWS book about C++ apps so I figure it might be also correct for
DELPHI.

Anyways thanks for trying guys :)

--
Lior Amar
Computer Consultant
SCii Technologies
lior.a...@sncf.fr

Other Threads