Coin3D(Open Inventor)中出现未处理的异常错误

Unhandled exception error in Coin3D ( Open Inventor )

本文关键字:未处理 异常 错误 Open Inventor Coin3D      更新时间:2023-10-16

我想制作一个通用函数,搜索节点中的类类型并返回其地址。其定义见

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();
}

但是,当我传递SoCoordinate3::getClassTypeId()这样的搜索类型和要搜索senode的节点时,如下所示:

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

但是最后一行生成了一个未处理的异常错误。请告诉我我在这里做错了什么。最后一行是有效的,因为在函数范围内编写的相同行可以工作,但不在这里。

这样一来,mysearch.getPath()可能为空:

if (mysearch.getPath() == NULL) { 
        std::cout<<"No property of this type was found";
    }

但下面你使用的是没有任何检查:

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

因此这可能会引发未处理的异常。

另一个点是这条线:

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

没有检查向量中有多少点,这也可能导致异常。