Opencascade。在C#中包装C ++指针时的AccessViolation

时间:2014-06-02 08:30:17

标签: c# c++-cli opencascade

我试图使用C#中的opencascade。我从使用OCCTProxy的.NET示例开始。 为了跟踪创建的形状,我使用这个类NativeWrapper来保存指向对象的指针。

public ref class NativeWrapper
{
  public:
     NativeWrapper();

     // Allocate the native object on the C++ Heap via a constructor
     NativeWrapper(void* topoDs) : m_Impl(topoDs) {}

     // Deallocate the native object on a destructor
     ~NativeWrapper() {
          delete m_Impl;
     }

  protected:
     // Deallocate the native object on the finalizer just in case no destructor is called
     !NativeWrapper() {
          delete m_Impl;
     }

  public:
     void* GetPointer() { return m_Impl; }

  private:
     void* m_Impl;
};

此处绘制形状,并将NativeWrapper设置为treenode上的标记

TopoDS_Shape MakeBox(Standard_Real x0, Standard_Real y0, Standard_Real z0, Standard_Real xDelta, Standard_Real yDelta, Standard_Real zDelta)
{
     gp_Pnt center(x0, y0, z0);
     BRepPrimAPI_MakeBox mkBox(center, xDelta, yDelta, zDelta);
     TopoDS_Shape shape = mkBox.Shape();
     return shape;
}

NativeWrapper^ DrawBox(Standard_Real x0, Standard_Real y0, Standard_Real z0, Standard_Real xDelta, Standard_Real yDelta, Standard_Real zDelta)
{
     TopoDS_Shape shape = MakeBox(x0, y0, z0, xDelta, yDelta, zDelta);
     myAISContext()->Display(new AIS_Shape(shape));
     TopoDS_Shape* retVal = new TopoDS_Shape(shape);
     NativeWrapper ^f = gcnew NativeWrapper(retVal);
     return f;
}     

private void ribbonButtonAddBox_Click(object sender, EventArgs e)
{
     var dlg = new DrawCubeDialog();
     dlg.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
     if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
          var data = dlg.CubeData;
          var o = Viewer.DrawCube(data.Name, data.Point, data.XDelta, data.YDelta, data.ZDelta);
          treeNodePrimitives.Nodes.Add(new TreeNode(data.Name) { Tag = o });
     }
}

但是当我尝试使用该对象时,我得到了这个异常

  

System.AccessViolationException未处理   Message =尝试读取或写入受保护的内存。这通常表明其他内存已损坏。   来源= OCCTProxy   堆栈跟踪:   at Handle_Standard_Transient.BeginScope(Handle_Standard_Transient *)   at Handle_Standard_Transient。{ctor}(Handle_Standard_Transient *,Handle_Standard_Transient * aTid)位于c:\ opencascade6.7.0 \ opencascade-6.7.0 \ inc \ handle_standard_transient.hxx:第82行   at Handle_MMgt_TShared。{ctor}(Handle_MMgt_TShared *,Handle_MMgt_TShared * aHandle)位于c:\ opencascade6.7.0 \ opencascade-6.7.0 \ inc \ handle_mmgt_tshared.hxx:第25行   at Handle_TopoDS_TShape。{ctor}(Handle_TopoDS_TShape *,Handle_TopoDS_TShape * aHandle)位于c:\ opencascade6.7.0 \ opencascade-6.7.0 \ inc \ handle_topods_tshape.hxx:第25行   在TopoDS_Shape。{ctor}(TopoDS_Shape *,TopoDS_Shape * A_0)   ...

NativeWrapper^ Partition(void* objA, void* objB)
{
     TopoDS_Shape* p_A = static_cast<TopoDS_Shape*>(objA);
     TopoDS_Shape* p_B = static_cast<TopoDS_Shape*>(objB);

     TopoDS_Shape a = *p_A;
     TopoDS_Shape b = *p_B;

     BOPAlgo_Builder aBuilder;
     aBuilder.AddArgument(a);
     aBuilder.AddArgument(b);
     aBuilder.Perform();
     Standard_Integer iErr = aBuilder.ErrorStatus();
     if (iErr)
     {
          // TODO: HANDLE ERROR
     }
     const TopoDS_Shape& shape = aBuilder.Shape();
     TopoDS_Shape retVal = shape;

     ClearView();
     Draw(retVal);

     NativeWrapper ^f = gcnew NativeWrapper(&retVal);
     return f;
}

任何人都可以向我解释这里出了什么问题,以及如何做到这一点?

任何帮助非常感谢:)

0 个答案:

没有答案