ItemHasChildren returns TRUE, then GetChildItem returns NULL 
Author Message
 ItemHasChildren returns TRUE, then GetChildItem returns NULL

Occasionly, after a call to CTreeCtrl::ItemHasChildren returns TRUE, a
subsequent call to GetChildItem returns NULL. What does this mean?

Here's a function I wrote to delete all the children of an item:

        if ( !pTree->ItemHasChildren(hItem) )
                return TRUE;

        HTREEITEM hNext;
        HTREEITEM hChild = pTree->GetChildItem(hItem);

        while ( hChild )
        {
                hNext = pTree->GetNextSiblingItem(hChild);
                pTree->DeleteItem(hChild);
                hChild = hNext;
        }

        return TRUE;

Sometimes, when the code gets down to assigning hChild to
GetChildItem(hItem), hChild is set to NULL. I can't figure out the
circumstances, or what it means, or whether or not my simple function
is wrong in some way... Any thoughts?

Mike Oliver



Sun, 10 Jul 2005 21:49:59 GMT  
 ItemHasChildren returns TRUE, then GetChildItem returns NULL

Quote:

>Occasionly, after a call to CTreeCtrl::ItemHasChildren returns TRUE, a
>subsequent call to GetChildItem returns NULL. What does this mean?

>Here's a function I wrote to delete all the children of an item:

>    if ( !pTree->ItemHasChildren(hItem) )
>            return TRUE;

>    HTREEITEM hNext;
>    HTREEITEM hChild = pTree->GetChildItem(hItem);

>    while ( hChild )
>    {
>            hNext = pTree->GetNextSiblingItem(hChild);
>            pTree->DeleteItem(hChild);
>            hChild = hNext;
>    }

>    return TRUE;

>Sometimes, when the code gets down to assigning hChild to
>GetChildItem(hItem), hChild is set to NULL. I can't figure out the
>circumstances, or what it means, or whether or not my simple function
>is wrong in some way... Any thoughts?

It looks like it ought to work. Is there any code or activity in other
threads you're not showing? Strictly speaking, you don't need the
ItemHasChildren test. GetChildItem will return NULL for "barren" nodes.

--
Doug Harrison
Microsoft MVP - Visual C++



Sun, 10 Jul 2005 22:25:07 GMT  
 ItemHasChildren returns TRUE, then GetChildItem returns NULL

Quote:
> It looks like it ought to work. Is there any code or activity in other
> threads you're not showing? Strictly speaking, you don't need the
> ItemHasChildren test. GetChildItem will return NULL for "barren" nodes.

No activity in other threads. The reason why I call ItemHasChildren is
that I've seen GetChildItem return a value when in fact the HTREEITEM
has no children, so I've always called them together, first calling
ItemHasChildren and then, if true, calling GetChildItem to begin the
iteration.

I'm still puzzled about what I've seen - ItemHasChildren returns TRUE,
but GetChildItem returns NULL. But, thanks for the help.

Mike Oliver



Mon, 11 Jul 2005 20:04:04 GMT  
 ItemHasChildren returns TRUE, then GetChildItem returns NULL

Quote:


>> It looks like it ought to work. Is there any code or activity in other
>> threads you're not showing? Strictly speaking, you don't need the
>> ItemHasChildren test. GetChildItem will return NULL for "barren" nodes.

>No activity in other threads. The reason why I call ItemHasChildren is
>that I've seen GetChildItem return a value when in fact the HTREEITEM
>has no children, so I've always called them together, first calling
>ItemHasChildren and then, if true, calling GetChildItem to begin the
>iteration.

>I'm still puzzled about what I've seen - ItemHasChildren returns TRUE,
>but GetChildItem returns NULL. But, thanks for the help.

FWIW, I'm not aware of any bugs in these treeview functions, and you're
describing two bugs. Searching google for +TVM_GETNEXTITEM +bug doesn't turn
up anything useful. That suggests the problem might be, as they say,
operator error. Frustrating, I know. I guess you might try sending the TVM
messages yourself, instead of using the MFC functions, but the latter wrap
the messages so thinly, I wouldn't expect there to be any mistakes. Beyond
that, I would continue looking hard for any mistakes I might have made, and
I'd try to narrow down when the "bugs" start to appear and take note of the
tree configuration and try to make a repro case.

--
Doug Harrison
Microsoft MVP - Visual C++



Mon, 11 Jul 2005 20:22:49 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. IsRectVisible returns false when it should return true.

2. ItemHasChildren returns 0

3. Fread returns 0, ferror returns 0, feof returns 16

4. internetautodial always return true

5. CFtpFileFind::FindFile always returns TRUE???

6. CWinApp::OnIdle: Always returning TRUE??

7. CInternetSession QueryOption function always return true.

8. SystemInformation.Network always returns TRUE

9. Typecast returns null

10. Allowing function to return double and/or NULL

11. Reflection -- Type.GetType() returns null

12. Passing char* to function returning null

 

 
Powered by phpBB® Forum Software