Article View: borland.public.cppbuilder.nativeapi
Article #2612Re: Memory Leak - TForm or TStringGrid or DynamicArray
From: Dan Mayo
Date: Mon, 05 Feb 2007 21:33
Date: Mon, 05 Feb 2007 21:33
130 lines
5374 bytes
5374 bytes
Dan Mayo wrote:
> I have created some test code that dynamically creates a form; on that
> form is a button and a string grid. The button click event fills in a
> number of static objects with random data; then prior to exiting the
> function posts all that data into the StringGrid; this should in effect
> clean up those objects when the click even expires leaving just the data
> in the grid. That much works.
>
> However, when I close the form, when is then followed by a delete
> operator, the memory doesn't appear to be freed (looking at the Virtual
> Size of the program via the task manager).
>
> Reading the TStringGrid help, it looks as though I need to delete the
> 'objects' in the stringgrid too? How? Is there something else I am
> missing?
>
> I'm still trying to track down the problem I am having where I can't
> create a new TForm at random times that I posted in "Creating new TForm
> causes 'Out of Resources' error on WinXP" a few weeks ago. It's not
> that my program is taking up much memory, or the leak I see in the above
> test app is that big. I'm grasping at straws to find out what is going
> on here; any help would be appreciated.
>
> Thanks.
>
>
The example code is as follows: (Note, MainForm, Form1's button1 event
contains a loop that dynamically creates a Form2 which is then deleted
after the modal operation is done; Form2 kicks off a timer when shown,
and the timer event does some processing, then returns a modal result;
Where is the memory leak? Of course the code operates faster when the
length of the dynamic array is defined before the first loop; the leak
is more pronounced (around 2k per pass) with the code as is; with the
length predefined, the leak appears to be around 2k per 10000 open and
closes.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainForm.h"
#include "SubForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TForm2 *NewForm = new TForm2(this);
NewForm->ShowModal();
delete NewForm;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
for (int i=0; i<ScrollBar1->Position; i++)
{
Button1->Click();
StatusBar1->SimpleText = i;
Application->ProcessMessages();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ScrollBar1Change(TObject *Sender)
{
Label1->Caption = ScrollBar1->Position;
}
//---------------------------------------------------------------------------
//SUBFORM
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "SubForm.h"
#include <math.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
StringGrid1->ColCount = 0;
StringGrid1->RowCount = 0;
}
//---------------------------------------------------------------------------
void TForm2::DoTest()
{
StringGrid1->ColCount = 100;
StringGrid1->RowCount = 100;
DynamicArray<double> vs;
for (int i =0; i<100; i++)
{
for (int j=0; j<100; j++)
{
vs.Length++;
vs[i+j] = rand();
}
}
for (int i =0; i<100; i++)
{
for (int j=0; j<100; j++)
{
StringGrid1->Cells[i][j]=vs[i+j];
}
}
ModalResult = mrOk;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Timer1Timer(TObject *Sender)
{
Timer1->Enabled = false;
DoTest();
}
//---------------------------------------------------------------------------
Technical Details
Message-ID:
<45c7a28a@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<45c7810f@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,191a7d21d69bde32 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Mon, 05 Feb 2007 15:33:06 -0600 From: Dan Mayo <dmayo@dickey-john.com> User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Memory Leak - TForm or TStringGrid or DynamicArray References: <45c7810f@newsgroups.borland.com> In-Reply-To: <45c7810f@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 216.174.165.174 Message-ID: <45c7a28a@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1170711178 216.174.165.174 (5 Feb 2007 13:32:58 -0800) Lines: 131 Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail