Board index » cppbuilder » Dynamic TPageControl, TTabSheet, TMemo crashes during deletes

Dynamic TPageControl, TTabSheet, TMemo crashes during deletes

I have no problem dynamically creating a TPageControl with a TTabSheet containing a TMemo component, but when I try
to exit, the system crashes.  The first error is that RemoveComponent fails to remove Memo, then it goes downhill.  IF I do
not put a TMemo component in, then there are no delete errors.
Thank you in advance for any assistance.
Here is the code:

void __fastcall TFwBuildForm::FileOpenExecute(TObject *Sender)
{
  OpenDialog->Execute();
  PageControl = new TPageControl(FwBuildForm);
  PageControl->Parent = this;

  TabSheet = new TTabSheet(PageControl);
  TabSheet->Parent = this;
  TabSheet->PageControl = PageControl;
  PageControl->ActivePage = TabSheet;
  TabSheet->TabVisible = true;
  TabSheet->Enabled = true;
  TabSheet->Caption = ExtractFileName(OpenDialog->FileName);
  TabSheet->Visible = true;
  TabSheet->Highlighted = true;

  Memo = new TMemo(TabSheet);
  Memo->Parent = this;
  TabSheet->InsertComponent(Memo);
  Memo->Enabled = true;
  Memo->Align = alClient;
  Memo->Visible = true;
  Memo->Show();

Quote
}

//---------------------------------------------------------------------------

void __fastcall TFwBuildForm::FileSaveExecute(TObject *Sender)
{
  SaveDialog->Execute();

Quote
}

//---------------------------------------------------------------------------

void __fastcall TFwBuildForm::FileExitExecute(TObject *Sender)
{
  Memo->Enabled = false;
  Memo->Visible = false;
  PageControl->ActivePage = NULL;
  TabSheet->TabVisible = false;
  TabSheet->Enabled = false;
  TabSheet->Visible = false;
  TabSheet->Highlighted = false;
  TabSheet->RemoveComponent(Memo);
delete Memo;
delete TabSheet;
delete PageControl;
Close();
//  Application->Terminate();

 

Re:Dynamic TPageControl, TTabSheet, TMemo crashes during deletes


Since you're deleting the tabsheet anyway, you don't need to remove the memo
manually from it first, the tabsheet will do that automatically.  If fact,
since you're deleting the entire tabcontrol as well, you don't need to
delete the individual tabsheets, they'll be freed automatically, which in
turn will automatically free the child controls automatically. In fact,
since you're closing the entire form anyway, you don't even need to free the
tabcontrol manually, either.  The form will free it.

The general rule of thumb is that if you assign an Owner when creating a
control dynamically at runtime, then you don't have to call 'delete'
yourself on that control, as the Owner will take care of freeing the control
for you.

So, in your case, you can simplify your code to this:

    void __fastcall TFwBuildForm::FileExitExecute(TObject *Sender)
    {
        Close();
    }

Gambit

Quote
"Steve Comee" <steven.l.co...@boeing.com> wrote in message

news:3b55d789$1_2@dnews...
Quote
> I have no problem dynamically creating a TPageControl with a TTabSheet
> containing a TMemo component, but when I try to exit, the system crashes.
> The first error is that RemoveComponent fails to remove Memo, then it goes
> downhill.  IF I do not put a TMemo component in, then there are no delete
> errors.

Re:Dynamic TPageControl, TTabSheet, TMemo crashes during deletes


Thank you for responding. What you describe it what I started out with, and was getting crashes upon exiting.  This morning
I found the cause - calling InsertComponent. Apparently all that is required is to set the Parent property.

Quote
"Remy Lebeau" <gambi...@gte.net> wrote:
>Since you're deleting the tabsheet anyway, you don't need to remove the memo>manually from it first, the tabsheet will do that automatically.  If fact,
>since you're deleting the entire tabcontrol as well, you don't need to
>delete the individual tabsheets, they'll be freed automatically, which in
>turn will automatically free the child controls automatically. In fact,
>since you're closing the entire form anyway, you don't even need to free the
>tabcontrol manually, either.  The form will free it.

>The general rule of thumb is that if you assign an Owner when creating a
>control dynamically at runtime, then you don't have to call 'delete'
>yourself on that control, as the Owner will take care of freeing the control
>for you.

>So, in your case, you can simplify your code to this:

>    void __fastcall TFwBuildForm::FileExitExecute(TObject *Sender)
>    {
>        Close();
>    }

>Gambit

>"Steve Comee" <steven.l.co...@boeing.com> wrote in message
>news:3b55d789$1_2@dnews...

>> I have no problem dynamically creating a TPageControl with a TTabSheet
>> containing a TMemo component, but when I try to exit, the system crashes.
>> The first error is that RemoveComponent fails to remove Memo, then it goes
>> downhill.  IF I do not put a TMemo component in, then there are no delete
>> errors.

Re:Dynamic TPageControl, TTabSheet, TMemo crashes during deletes


Setting Parent automatically calls InsertComponent internally.  So, what was
most likely happening was that you were adding a reference to the component
twice - once when Parent was set and again when you were calling
InsertComponent yourself (which does NOT check for duplicate references,
btw).  so, when the Parent frees it's child components, the first reference
was used to free the component correctly, but the second reference would
then reference to invalid memory, so when the Parent reached tried to free
that, it would crash.

Gambit

Quote
"Steve Comee" <steven.l.co...@boeing.com> wrote in message

news:3b571bde$1_1@dnews...
Quote
> This morning I found the cause - calling InsertComponent. Apparently all
> that is required is to set the Parent property.

Other Threads