Board index » cppbuilder » TreeNodes->Clear() doesn't work in destructor of form

TreeNodes->Clear() doesn't work in destructor of form

I have created a new app (in BCB5 patch 1) and added

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    TreeView1->Items->Add(NULL, "Hello");
int i = TreeView1->Items->Count;
    TreeView1->Items->Clear();
int j = TreeView1->Items->Count;

Quote
}

__fastcall TForm1::~TForm1(void)
{
    TreeView1->Items->Add(NULL, "Hello");
int i = TreeView1->Items->Count;
    TreeView1->Items->Clear();
int j = TreeView1->Items->Count;

Quote
}

I put a break point on the closing brace of both constructor and destructor.
After the constructor, i = 1 and j = 0 as expected.  But at the end of the
destructor, i = 1 and j = 1.  The clear hasn't worked.  Is it supposed to
behave in this way?  It seems like a bug to me.  Any ideas?

I know I don't have to do this in the destructor, but I have a general clean
up function that is called by the destructor of a form and I don't see why I
can't call clear as the next piece of code works perfectly.

At the moment I've had to perform a loop to delete the tree nodes:

while (TreeView1->Items->Count)
{
    TreeView1->Items->Delete(TreeView1-Items->Item[0]);

Quote
}

This isn't the best way, though.  I have turned on debug libraries and tried
stepping in to the source code, but it just skips over that line. (I do have
the proffesional version, and I can usually step in to the source).

Russell

--
=================================================================
Mail:   Russell Hind                              #####
        The Technology Partnership PLC            #####
        Melbourn, Royston, Herts.                 #####
        SG8 6EE. United Kingdom                   #####
Tel:    +44 (1763) 262 626                        #####
Fax:    +44 (1763) 261 582             The Technology Partnership
e-mail: russell.h...@techprt.co.uk
=================================================================

 

Re:TreeNodes->Clear() doesn't work in destructor of form


Russell:

Your answer lies in the implementation of the TTreeNodes.Clear
method itself (see ComCtrls.pas unit in Source\VCL directory):

  procedure TTreeNodes.Clear;
  begin
    ClearCache;
    if not (csDestroying in Owner.ComponentState) and
            Owner.HandleAllocated then
      TreeView_DeleteAllItems(Owner.Handle);
  end;

As you can see, if the component state of the owner (the TreeView
control itself) is not being destroyed (which it is in your form's
destructor, but not in the OnDestroy event handler), then it deletes
all the items.  Otherwise, it doesn't -- which is what you're seeing.

Hope this helps,
-Jim Rofkar.
jrof...@cros.net

Quote
"Russell Hind" <r...@techprt.co.uk> wrote in message

news:39a530fb$1_2@dnews...
Quote
> I have created a new app (in BCB5 patch 1) and added

> __fastcall TForm1::TForm1(TComponent* Owner)
>     : TForm(Owner)
> {
>     TreeView1->Items->Add(NULL, "Hello");
> int i = TreeView1->Items->Count;
>     TreeView1->Items->Clear();
> int j = TreeView1->Items->Count;
> }

> __fastcall TForm1::~TForm1(void)
> {
>     TreeView1->Items->Add(NULL, "Hello");
> int i = TreeView1->Items->Count;
>     TreeView1->Items->Clear();
> int j = TreeView1->Items->Count;
> }

> I put a break point on the closing brace of both constructor and
destructor.
> After the constructor, i = 1 and j = 0 as expected.  But at the end of the
> destructor, i = 1 and j = 1.  The clear hasn't worked.  Is it supposed to
> behave in this way?  It seems like a bug to me.  Any ideas?

> I know I don't have to do this in the destructor, but I have a general
clean
> up function that is called by the destructor of a form and I don't see why
I
> can't call clear as the next piece of code works perfectly.

> At the moment I've had to perform a loop to delete the tree nodes:

> while (TreeView1->Items->Count)
> {
>     TreeView1->Items->Delete(TreeView1-Items->Item[0]);
> }

> This isn't the best way, though.  I have turned on debug libraries and
tried
> stepping in to the source code, but it just skips over that line. (I do
have
> the proffesional version, and I can usually step in to the source).

> Russell

> --
> =================================================================
> Mail:   Russell Hind                              #####
>         The Technology Partnership PLC            #####
>         Melbourn, Royston, Herts.                 #####
>         SG8 6EE. United Kingdom                   #####
> Tel:    +44 (1763) 262 626                        #####
> Fax:    +44 (1763) 261 582             The Technology Partnership
> e-mail: russell.h...@techprt.co.uk
> =================================================================

Other Threads