tags:

views:

52

answers:

1

I'm doing the following and getting that debug error:

AguiWidgetBase* AguiWidgetContainer::recursiveGetWidgetUnderMouse(
    AguiWidgetBase* root, const AguiMouseEventArgs &mouse)
{

    AguiWidgetBase* currentNode = root;
    bool foundsomething = true;

    while(foundsomething)
    {
        foundsomething = false;
        if(currentNode->getChildCntrolCount() > 0)
            for (std::vector<AguiWidgetBase*>::const_reverse_iterator rit =
                    currentNode->getChildRBeginIterator();
                rit < currentNode->getChildREndIterator(); ++rit) 
            { 
                if(!foundsomething)
                    if ((*rit)->intersectionWithPoint(mouse.getPosition())) 
                    { 
                        foundsomething = true;
                        currentNode = *rit;
                    } 

                } 
            }
            return currentNode;
        }

    // ...

It fails after currentNode becomes a pointer to a child of root, and crashes on the for.

What am I doing wrong?

Thanks

+1  A: 

You are reassigning the end position which is being checked by your iterator.

2 comments:

  1. Put the brackets around your if statements. You're allowed not to do it but it makes the code more readable if you do and reminds you to indent.

  2. add a break statement to the inner if so that you don't need to continue traversing the vector. That will accomplish exactly what you want without the extra variable and variable check.

wheaties
how could I fix this?
Milo
@Milo - see my edit. Add a break statement in the inner if statement so that you short circuit your for loop. However, don't reassign the iterator to begin with. If you feel you must, use a different variable!
wheaties