Why does my program crash when the program exits?

I write a program to find the intersections of 2 B-Spline-Surfaces by using Geom_BSplineSurface and GeomAPI_IntSS. The program always crashes in opencascade::handle::EndScope() when the main function returns 0. Once I delete the related code the program will not crash. What's the problem in the program?

My code is following:

#include <GeomAPI_IntSS.hxx>
#include <Geom_BSplineSurface.hxx>
#include <Geom_BSplineCurve.hxx>

int main()
{
    TColgp_Array2OfPnt points1(1, 4, 1, 4);
    points1.SetValue(1, 1, gp_Pnt(-10, 10, 0));
    points1.SetValue(1, 2, gp_Pnt(-4.44, 10.53, 10.73));
    points1.SetValue(1, 3, gp_Pnt(3.88, 10.76, 12));
    points1.SetValue(1, 4, gp_Pnt(10, 10, 0));
    points1.SetValue(2, 1, gp_Pnt(-10.86, 4.78, 7.6));
    points1.SetValue(2, 2, gp_Pnt(-5.61, 5.09, 13.31));
    points1.SetValue(2, 3, gp_Pnt(4.89, 5.17, 14.49));
    points1.SetValue(2, 4, gp_Pnt(10.71, 4.86, 9.00));
    points1.SetValue(3, 1, gp_Pnt(-10.78, -4.8, 7.65));
    points1.SetValue(3, 2, gp_Pnt(-5.45, -5.09, 13.00));
    points1.SetValue(3, 3, gp_Pnt(4.96, -5.25, 13.83));
    points1.SetValue(3, 4, gp_Pnt(10.48, -5.01, 8.46));
    points1.SetValue(4, 1, gp_Pnt(-10, -10, 0));
    points1.SetValue(4, 2, gp_Pnt(-4.28, -10.76, 11.98));
    points1.SetValue(4, 3, gp_Pnt(3.29, -10.69, 10.88));
    points1.SetValue(4, 4, gp_Pnt(10, -10, 0));
    TColStd_Array1OfReal uKnots1(1, 2);
    uKnots1.SetValue(1, 0);
    uKnots1.SetValue(2, 32.2776);
    TColStd_Array1OfReal vKnots1(1, 2);
    vKnots1.SetValue(1, 0);
    vKnots1.SetValue(2, 32.2776);
    TColStd_Array1OfInteger uMults1(1, 2);
    uMults1.SetValue(1, 4);
    uMults1.SetValue(2, 4);
    TColStd_Array1OfInteger vMults1(1, 2);
    vMults1.SetValue(1, 4);
    vMults1.SetValue(2, 4);
    Geom_BSplineSurface surf1(points1, uKnots1, vKnots1, uMults1, vMults1, 3, 3);

    double H = 6.15;
    TColgp_Array2OfPnt points2(1, 2, 1, 2);
    points2.SetValue(1, 1, gp_Pnt(-20, 20, H));
    points2.SetValue(1, 2, gp_Pnt(20, 20, H));
    points2.SetValue(2, 1, gp_Pnt(-20, -20, H));
    points2.SetValue(2, 2, gp_Pnt(20, -20, H));
    TColStd_Array1OfReal uKnots2(1, 2);
    uKnots2.SetValue(1, 0);
    uKnots2.SetValue(2, 40);
    TColStd_Array1OfReal vKnots2(1, 2);
    vKnots2.SetValue(1, 0);
    vKnots2.SetValue(2, 40);
    TColStd_Array1OfInteger uMults2(1, 2);
    uMults2.SetValue(1, 2);
    uMults2.SetValue(2, 2);
    TColStd_Array1OfInteger vMults2(1, 2);
    vMults2.SetValue(1, 2);
    vMults2.SetValue(2, 2);
    Geom_BSplineSurface surf2(points2, uKnots2, vKnots2, uMults2, vMults2, 1, 1);

    // Once delete the following lines the program works successfully
    // But the program does not find the intersections anymore
    Handle(Geom_Surface) h1 = &surf1;
    Handle(Geom_Surface) h2 = &surf2;

    GeomAPI_IntSS intersector(h1, h2, 1e-7);

    if (!intersector.IsDone())
    {
        printf("not done\n");
        return -1;
    }
    printf("Got %d curve(s)\n", intersector.NbLines());
    for (int i = 1; i <= intersector.NbLines(); i++)
    {
        Handle(Geom_Curve) curve = intersector.Line(i);
        int mmm = 0;
    }

    return 0;
}
Kirill Gavrilov's picture
    Geom_BSplineSurface surf2(points2, uKnots2, vKnots2, uMults2, vMults2, 1, 1);

    // Once delete the following lines the program works successfully
    // But the program does not find the intersections anymore
    Handle(Geom_Surface) h1 = &surf1;
    Handle(Geom_Surface) h2 = &surf2;

This code is broken - you cannot allocate object on a stack and then put into a smart-pointer. You should initialize Handle objects with new keyword.

Li Deng's picture

Thanks a lot. I've fixed the bugs.