多次更新后Vector_2销毁期间的分段错误

时间:2013-11-24 00:02:41

标签: c++ segmentation-fault computational-geometry cgal

我编写了一个程序,使用 CGAL :: Vector_2 对象和 CGAL :: Exact_predicates_exact_constructions_kernel 进行数据表示。

基本上,代码有一个包含 CGAL :: Vector_2 的结构,我在执行期间通过添加许多其他CGAL向量来更新。问题是取决于我对结构中的向量执行了多少次,稍后,在对象删除期间会生成以下分段错误

  

编程接收信号SIGSEGV,分段故障。   CGAL中的0x0000000000409693 :: Lazy_rep_3GAL :: Interval_nt> >,CGAL :: CartesianKernelFunctors :: Construct_vector_2>,CGAL :: Cartesian_converter,CGAL :: Simple_cartesian>,CGAL :: NT_converter> >,CGAL :: Return_base_tag,int,int> :: ~Lazy_rep_3()()

我已经能够凭经验检查每次我在结构中执行超过47961次向量的添加时产生的错误。 如果我做的更少,则对象被正确破坏并且程序正确完成。

我试图跟踪GDB的问题,但这只会让我发现问题是在矢量删除过程中发生的。

此时我想知道我是否在我的代码中某处搞砸了,或者这可能只是库中的错误。

以下是代码的副本:

#include <iostream>
#include <stdio.h>

// CGAL library functions & classes
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Vector_2.h>  

// NULL VECTOR definition
#define NULL_VECTOR CGAL::NULL_VECTOR

// SCENE ORIGIN definition
#define SCENE_ORIGIN CGAL::ORIGIN

// CGAL types definition
typedef CGAL::Exact_predicates_exact_constructions_kernel Rep;
typedef CGAL::Vector_2<Rep> Vector;


// Structure that stores internally a CGAL::Vector<2> object
// which is initialized as CGAL::NULL_VECTOR
struct foo
{
    // Attributes
    std::string id;
    Vector vector;

    // Constructor
    foo() : id("foo"), vector(NULL_VECTOR) 
    {
        std::cout << "[ foo() ]: Creating foo object at " << this << std::endl;
        this->print_info("foo()");
    }

    // Copy constructor
    foo(const foo& f) : id("foo_copy"), vector(NULL_VECTOR)
    {
        std::cout << "[ foo(const foo&) ]: Coping foo object at " << &f << " to foo object at " << this << std::endl;
        this->print_info("foo(const foo&)");
    }

    // Destructor
    ~foo()
    {
        std::cout << "[ ~foo() ]: Deleting foo object at " << this << std::endl;
        this->print_info("~foo()");
    }

    void print_info(std::string func_name)
    {
        std::string func_id = "[ " + func_name + " ]: ";
        std::cout << func_id << "foo.id memory position = " << &(this->id) << std::endl
                  << func_id << "foo.id value = " << this->id << std::endl
                  << func_id << "foo.vector memory position: " << &(this->vector) << std::endl
                  << func_id << "foo.vector value: ";
        printf("(%.2f, %.2f)\n\n", CGAL::to_double(this->vector.x()) ,CGAL::to_double(this->vector.y()));
    }
};


// foo_function receives by copy a foo object and through a loop
// will increase repeatedly the value of foo's inner vector.
void foo_func(foo f, int iterations)
{
    std::cout << "[ foo_function(foo) ]: Started" << std::endl; 
    for(int i=0; i<iterations; i++)
    {
        for (int j=0; j<iterations; j++)
        {
            printf("%c[s\r[ foo_funtion(foo) ]: Running iteration (i=%d,j=%d)%c[u",033,i,j,033);
            Vector update(i,j);
            f.vector = f.vector + update;
        }
    }
    std::cout << "\n[ foo_function(foo) ]: Finished\n" << std::endl;
}

int main (int argc, char* argv[])
{
    int iterations = 219; 
    //219 is the fist value for iterations
    //that produces the seg fault

    if (argc > 1)
        iterations = atoi(argv[1]);

    // Creating a foo object
    foo f;

    // Calling foo_function to update it inner vector 'iteration' times
    std::cout << "[ main() ]: Calling foo_function()\n" << std::endl;
    foo_func(f, iterations);
    std::cout << "[ main() ]: Returned from foo_function()\n" << std::endl;

    return 0;
}

这是我用来编译它的行:

  

g ++ -frounding-math -pedantic -Wall -Wextra -fPIC -lCGAL -lmpfr -lgmp -lboost_thread cgal_vector_error.cpp

如前一行所示,它需要运行以下库:

  • CGAL
  • MPFR
  • GMP
  • Boost.Thread

非常感谢你的帮助。

此致

0 个答案:

没有答案