Question

I wanted to make a general function that would search for a class type in a node and returns its address. It is defined below

SoNode* searchandgive(SoType searchtype, SoNode* searchnode)
{
    SoSearchAction mysearch;
    mysearch.setType(searchtype);
    mysearch.setInterest(SoSearchAction::FIRST);
    mysearch.apply(searchnode);
    if (mysearch.getPath() == NULL) { 

        std::cout<<"No property of this type was found";
    }

    SoPath* mypath=mysearch.getPath();
    return mypath->getTail();
}

But when I pass a search type like SoCoordinate3::getClassTypeId() and the node to be searched for senode as given below:

 SoCoordinate3 * mycoords=(SoCoordinate3*) searchandgive(SoCoordinate3::getClassTypeId(),senode);
 const SbVec3f *s=mycoords->point.getValues(0);
 std::cout<<"   " <<s->getValue()[25];  // Some point

But the last line is generating a Unhandled Exception Error. Please tell what am I doing wrong here. The last line is valid since the same written inside the scope of the function works but not here.

No correct solution

OTHER TIPS

With this you are standing that mysearch.getPath() could be null:

if (mysearch.getPath() == NULL) { 

        std::cout<<"No property of this type was found";
    }

but below you are using that without any check:

SoPath* mypath=mysearch.getPath();
    return mypath->getTail();

so this can raise an Unhandled Exception.

Another poitn is the line:

std::cout<<"   " <<s->getValue()[25];  // Some point

There is no check about how many points are in the vector, and this as well could cause an exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top