Geom2dGcc_Lin2d2Tan crashes in the destructor

Hello everyone!

I need to find a point on curve that is laying on tangent line and passing thru the given point.

To do that I use Geom2dGcc_Lin2d2Tan. But it crashes in the destructor with debug assertion failed (at the ~NCollection_Array1). 

Here is the example code:

    {
        gp_Pnt2d location(4340.30112754735, 1807.297924155403);
        gp_Circ2d circ2d(gp::OX2d(), 456.7692292696409);
        circ2d.SetLocation(location);

        double staangle = 0.4075093597596729;
        double endangle = 2.228364398074679;

        Handle(Geom2d_TrimmedCurve) arc = GCE2d_MakeArcOfCircle(circ2d, staangle, endangle);
        TopoDS_Edge arcEdge = BRepBuilderAPI_MakeEdge2d(arc);
        BRepLib::BuildCurve3d(arcEdge);
        Handle(AIS_Shape) arcShape = new AIS_Shape(arcEdge);

        occtContext()->Display(arcShape, false);

        gp_Pnt firstPoint(3708.7083560068013, 2735.1782535912234, 0.0);
        gp_Pln plane = gp_Pln(gp::Origin(), gp::DZ());

        const TopoDS_Edge &edge = TopoDS::Edge(arcShape->Shape());

        Standard_Real umin, umax;
        Handle(Geom_Curve) curve = BRep_Tool::Curve(edge, umin, umax);

        const Handle(Geom2d_Curve) curve2d = GeomAPI::To2d(curve, plane);
        gp_Pnt2d p2d = ProjLib::Project(plane, firstPoint);

        Geom2dAdaptor_Curve adaptor(curve2d);
        Geom2dGcc_QualifiedCurve qualifiedCurve(adaptor, GccEnt_unqualified);
        Geom2dGcc_Lin2d2Tan lin2d2Tan(qualifiedCurve, p2d, Precision::Confusion());

        if (lin2d2Tan.IsDone() && lin2d2Tan.NbSolutions()>0) {

            gp_Pnt2d tp;
            double u_sol, u_arg;
            lin2d2Tan.Tangency1(1, u_sol, u_arg, tp);

            gp_Pnt tangentPoint = ElSLib::Value(tp.X(), tp.Y(), plane);

            TopoDS_Vertex vertex = BRepBuilderAPI_MakeVertex(tangentPoint);
            Handle(AIS_Shape) vertexShape = new AIS_Shape(vertex);

            occtContext()->Display(vertexShape, false);
        }
    }

Any help is appreciated.

P.S. I found this thread: https://www.opencascade.com/content/geom2dgcclin2d2tan and I think that it is the same problem.And also the trouble appears only in release version of OCCT. 

Kirill Gavrilov's picture

I don't get any crash with your sample code on 7.1.0 and current master in Debug mode.
Which version are you using?

Are you sure you can reproduce the problem with isolated sample?
It might be the memory corruption in the code before.

Anton Shabalin's picture

I got the error on 7.1.0 Release mode.

Ok, I will try to reproduce it in the isolated sample.

Lincoln Nxumalo's picture

You can work around it by creating it in heap memory : Geom2dGcc_Lin2d2Tan* lin2d2Tan = new Geom2dGcc_Lin2d2Tan (qualifiedCurve, p2d, Precision::Confusion());