将TopoDS_Face对象分配给其子对象可编译,没有错误,但是我有3个valgrind错误 [英] Assigning a TopoDS_Face object to its child object compiles with no errors but I have 3 valgrind errors

查看:66
本文介绍了将TopoDS_Face对象分配给其子对象可编译,没有错误,但是我有3个valgrind错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个称为Test的类,它继承了TopoDS_Face类。已经从这获得了一些提示问题,但是...

  // Test.h 
class Test:公共TopoDS_Face
{
public:
void运算子=(const TopoDS_Face& base_)
{
TopoDS_Face :: operator =(base_);
}
}

// testmain.cpp
...
int main(){
//从IGES面孔中提取面孔
for(int i = 1; i< = nbs; i ++){
TopoDS_Shape shape = myIgesReader.Shape(i);
TopoDS_Face&脸= static_cast< TopoDS_Face&>(TopoDS :: Face(shape));
测试* test;
//也尝试了每种方法,但未成功
//测试* test = dynamic_cast< Test *>(& face);
// test =& face

test-> TopoDS_Face :: operator =(face);
...
}

}

main中的代码从IGES文件中提取人脸,并将每个人脸对象分配给main中创建的测试对象。编译进行得很好,但是valgrind抱怨以下错误:

  == 21718 ==使用大小为8 $的未初始化值b $ b == 21718 ==在0x9B0946C:Handle_Standard_Transient :: Assign(Standard_Transient const *)(在/usr/lib64/libTKernel.so.10.0.0中)
== 21718 == by 0x408111:Handle_TopoDS_TShape ::运算符=(Handle_TopoDS_TShape const&)(在/ home / ub / Projects / C ++ / test / test中)
== 21718 ==由0x409478:TopoDS_Shape :: operator =(TopoDS_Shape const&)(在/ home / ub中/ Projects / C ++ / test / test)
== 21718 ==通过0x4094CA:TopoDS_Face :: operator =(TopoDS_Face const&)(在/ home / ub / Projects / C ++ / test / test)
== 21718 ==通过0x407273:main(在/ home / ub / Projects / C ++ / test / test中)
== 21718 ==
== 21718 ==使用大小为8的未初始化值b $ b == 21718 == at 0x9B09431:Handle_Standard_Transient :: EndScope()(在/usr/lib64/libTKernel.so.10.0.0中)
== 21718 == by 0x9B09475:Handle_Standard_Transient :: Assign(Standard_Transient const *)(在/ usr / lib中64 / libTKernel.so.10.0.0)
== 21718 ==通过0x408111:Handle_TopoDS_TShape :: operator =(Handle_TopoDS_TShape const&)(在/ home / ub / Projects / C ++ / test / test)
== 21718 ==通过0x409478:TopoDS_Shape :: operator =(TopoDS_Shape const&)(在/ home / ub / Projects / C ++ / test / test)中
== 21718 ==通过0x4094CA:TopoDS_Face :: operator =(TopoDS_Face const&)(在/ home / ub / Projects / C ++ / test / test)
== 21718 ==通过0x407273:main(在/ home / ub / Projects / C ++ / test / test)
== 21718 ==
== 21718 ==大小为4的无效读取
== 21718 == at 0x9B0943C:Handle_Standard_Transient :: EndScope()(在/usr/lib64/libTKernel.so中.10.0.0)
== 21718 ==通过0x9B09475:Handle_Standard_Transient :: Assign(Standard_Transient const *)(在/usr/lib64/libTKernel.so.10.0.0中)
== 21718 ==通过0x408111:Handle_TopoDS_TShape :: operator =(Handle_TopoDS_TShape const&)(在/ home / ub / Projects / C ++ / test / test)中
== 21718 ==通过0x409478:TopoDS_Shape :: operator =(TopoDS_Shape const&) (在/ home / ub / Projects / C ++ / test / te中st)
== 21718 ==通过0x4094CA:TopoDS_Face :: operator =(TopoDS_Face const&)(在/ home / ub / Projects / C ++ / test / test)中
== 21718 ==通过0x407273 :main(在/ home / ub / Projects / C ++ / test / test中)
== 21718 ==地址0x75eb394801c38350没有被堆栈,malloc或(最近)被释放
== 21718 ==
== 21718 ==
== 21718 ==进程终止于信号11(SIGSEGV)的默认动作
== 21718 ==常规保护故障
== 21718 ==在0x9B0943C:Handle_Standard_Transient :: EndScope()(在/usr/lib64/libTKernel.so.10.0.0中)
== 21718 == by 0x9B09475:Handle_Standard_Transient :: Assign(Standard_Transient const *)(in /usr/lib64/libTKernel.so.10.0.0)
== 21718 ==通过0x408111:Handle_TopoDS_TShape :: operator =(Handle_TopoDS_TShape const&)(在/ home / ub / Projects / C ++ / test / test)
== 21718 ==通过0x409478:TopoDS_Shape :: operator =(TopoDS_Shape const&)(在/ home / ub / Projects / C ++ / test / test)中
== 21718 ==通过0x4094CA:TopoDS_Face :: operator =(TopoDS_Face const&)(在/ home / ub / Projects / C ++ / test / test)
== 21718 ==通过0x407273:main(在/ home / ub / Projects / C ++ / test / test)
== 21718 ==

我需要解决该程序的帮助。

解决方案

如果您查看 TopoDS_Shape 层次结构定义,您会注意到:




  • TopoDS_Shape 不是接口(既没有虚拟方法也没有虚拟析构函数)。 / p>


  • TopoDS_Face,TopoDS_Shell等提供了来自TopoDS_Shape的虚拟继承,而无需添加任何字段或方法。


  • OCCT不存储强类型的TopoDS_形状对象。 OCCT集合(首先包括TopoDS_Shape的子形状!)按值(而不是引用)存储TopoDS_Shape。像 TopoDS :: Face()这样的方法基本上会进行内存别名,以根据TopoDS_Shape :: ShapeType()方法返回的枚举,通过运行时检查返回强类型对象。


  • 实际上可共享的引用以TopoDS_Shape类的Handle(TopoDS_TShape)属性的形式存储。 TopoDS_TShape 是定义主要形状数据(几何形状)的真实类,并且确实是具有真实子类的虚拟类。


  • TopoDS_TShape 层次结构(具有诸如TopoDS_TShape,TopoDS_TShell等子类的层次结构)是内部的和最终的-没有接口可以在应用程序级别扩展此层次结构。我想说的是,在C ++ 11中,这些类应被标记为final。




对于此描述,应该清楚的是,更改TopoDS_Shape / TopoDS_TShape层次结构与OCCT设计(对于这些特定类)相矛盾,并且不能在应用程序级别可靠地完成。假定的替代方法是使用地图(例如 NCollection_DataMap< TopoDS_Shape,TopTools_ShapeMapHasher,MyShapeProperties> )或文档结构(例如 XCAF 文档)存储与形状相关的其他信息定义具有 TDataStd_NamedData 和其他属性的程序集结构。


I have a class called Test which inherits the TopoDS_Face class. Already got some tips from This question but...

    // Test.h
    class Test : public TopoDS_Face
    {
      public:
         void operator = (const TopoDS_Face& base_)
         {
            TopoDS_Face::operator=(base_);
         }
    }

    // testmain.cpp 
    ...
    int main() {
      //extract faces from IGES face
      for (int i = 1; i <= nbs; i++) {
        TopoDS_Shape shape = myIgesReader.Shape(i);
        TopoDS_Face& face = static_cast<TopoDS_Face&>(TopoDS::Face(shape));
        Test *test;
        // tried each of these also and did not succeed
        // Test *test = dynamic_cast<Test*>(&face);
        // test = &face

        test->TopoDS_Face::operator=(face);
        ...
    }

}

The code in main extracts faces from an IGES file and assigns each face object to the test object created in main. The compilation goes on well but valgrind complains with the following error:

==21718== Use of uninitialised value of size 8
==21718==    at 0x9B0946C: Handle_Standard_Transient::Assign(Standard_Transient const*) (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x408111: Handle_TopoDS_TShape::operator=(Handle_TopoDS_TShape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x409478: TopoDS_Shape::operator=(TopoDS_Shape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x4094CA: TopoDS_Face::operator=(TopoDS_Face const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x407273: main (in /home/ub/Projects/C++/test/test)
==21718== 
==21718== Use of uninitialised value of size 8
==21718==    at 0x9B09431: Handle_Standard_Transient::EndScope() (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x9B09475: Handle_Standard_Transient::Assign(Standard_Transient const*) (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x408111: Handle_TopoDS_TShape::operator=(Handle_TopoDS_TShape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x409478: TopoDS_Shape::operator=(TopoDS_Shape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x4094CA: TopoDS_Face::operator=(TopoDS_Face const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x407273: main (in /home/ub/Projects/C++/test/test)
==21718== 
==21718== Invalid read of size 4
==21718==    at 0x9B0943C: Handle_Standard_Transient::EndScope() (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x9B09475: Handle_Standard_Transient::Assign(Standard_Transient const*) (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x408111: Handle_TopoDS_TShape::operator=(Handle_TopoDS_TShape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x409478: TopoDS_Shape::operator=(TopoDS_Shape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x4094CA: TopoDS_Face::operator=(TopoDS_Face const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x407273: main (in /home/ub/Projects/C++/test/test)
==21718==  Address 0x75eb394801c38350 is not stack'd, malloc'd or (recently) free'd
==21718== 
==21718== 
==21718== Process terminating with default action of signal 11 (SIGSEGV)
==21718==  General Protection Fault
==21718==    at 0x9B0943C: Handle_Standard_Transient::EndScope() (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x9B09475: Handle_Standard_Transient::Assign(Standard_Transient const*) (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x408111: Handle_TopoDS_TShape::operator=(Handle_TopoDS_TShape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x409478: TopoDS_Shape::operator=(TopoDS_Shape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x4094CA: TopoDS_Face::operator=(TopoDS_Face const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x407273: main (in /home/ub/Projects/C++/test/test)
==21718== 

I need help with solving the program. Thanks in advance.

解决方案

If you will take a look into TopoDS_Shape hierarchy definition you will notice that:

  • TopoDS_Shape is not an interface (there are no virtual methods nor virtual destructor).

  • TopoDS_Face, TopoDS_Shell and others provide 'dummy' inheritance from TopoDS_Shape without adding any fields or methods.

  • OCCT doesn't store strongly typed TopoDS_ shape object. OCCT collections (including sub-shapes of TopoDS_Shape, first of all!) store TopoDS_Shape by value, not reference. Methods like TopoDS::Face() basically does memory aliasing to return strongly typed object with runtime check based on enumeration returned by TopoDS_Shape::ShapeType() method.

  • Actually sharable reference is stored in form of Handle(TopoDS_TShape) property of TopoDS_Shape class. TopoDS_TShape is a real class defining main shape data (geometry), and it is indeed virtual class with real sub-classes.

  • TopoDS_TShape hierarchy (with subclasses like TopoDS_TShape, TopoDS_TShell and others) is internal and final - there is no interface for extending this hierarchy at application level. I would say, that within C++11, these classes are expected to be marked as final.

Basing on this description, it should be clear that altering TopoDS_Shape / TopoDS_TShape hierarchy contradicts to OCCT design (for these specific classes) and cannot be done reliably at application level. Supposed alternative is storing additional information associated to shape using map (like NCollection_DataMap<TopoDS_Shape,TopTools_ShapeMapHasher,MyShapeProperties>) or by document structures (like XCAF document defining assembly structure with TDataStd_NamedData and other attributes).

这篇关于将TopoDS_Face对象分配给其子对象可编译,没有错误,但是我有3个valgrind错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆