Memory management 使用点云库处理点云时出现看似随机的崩溃-可能是内存问题?

Memory management 使用点云库处理点云时出现看似随机的崩溃-可能是内存问题?,memory-management,crash,point-cloud-library,Memory Management,Crash,Point Cloud Library,我正在执行点云重投影的几个步骤(最初约为4000万点,处理时约为2000万点)。程序在这两个循环中的任意一个点崩溃。如果我使用较小的子集(约1000万个点)运行它,一切正常 //Projection of Point Cloud into a sphere pcl::PointCloud<pcl::PointXYZ>::Ptr projSphere(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,int radius)

我正在执行点云重投影的几个步骤(最初约为4000万点,处理时约为2000万点)。程序在这两个循环中的任意一个点崩溃。如果我使用较小的子集(约1000万个点)运行它,一切正常

//Projection of Point Cloud into a sphere 
pcl::PointCloud<pcl::PointXYZ>::Ptr projSphere(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,int radius) 
        { 
        //output cloud 
        pcl::PointCloud<pcl::PointXYZ>::Ptr output(new pcl::PointCloud<pcl::PointXYZ>); 
        //time marker 
        int startTime = time(NULL); 
        cout<<"Start Sphere Projection"<<endl; 
        //factor by which each Point Vector ist multiplied to get a distance of radius to the origin 
        float scalar; 
        for (int i=0;i<cloud->size();i++) 
                { 
                if (i%1000000==0) cout<<i<<endl; 
                //P 
                pcl::PointXYZ tmpin=cloud->points.at(i); 
                //P' 
                pcl::PointXYZ tmpout; 
                scalar=radius/(sqrt(pow(tmpin.x,2)+pow(tmpin.y,2)+pow(tmpin.z,2))); 
                tmpout.x=tmpin.x*scalar; 
                tmpout.y=tmpin.y*scalar; 
                tmpout.z=tmpin.z*scalar; 
                //Adding P' to the output cloud 
                output->push_back(tmpout); 
                } 
        cout<<"Finished projection of "<<output->size()<<" points in "<<time(NULL)-startTime<<" seconds"<<endl; 
        return(output); 
        } 
//Stereographic Projection 
pcl::PointCloud<pcl::PointXYZ>::Ptr projStereo(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud) 
        { 
        //output cloud 
        pcl::PointCloud<pcl::PointXYZ>::Ptr outputSt(new pcl::PointCloud<pcl::PointXYZ>); 
        //time marker 
        int startTime = time(NULL); 
        cout<<"Start Stereographic Projection"<<endl; 
        for (int i=0;i<cloud->size();i++) 
                { 
                //P 
                if (i%1000000==0) cout<<i<<endl; 
                pcl::PointXYZ tmpin=cloud->points.at(i); 
                //P' 
                pcl::PointXYZ tmpout; 
                //equation 
                tmpout.x=tmpin.x/(1.0+tmpin.z); 
                tmpout.y=tmpin.y/(1.0+tmpin.z); 
                tmpout.z=0; 
                //Adding P' to the output cloud 
                outputSt->push_back(tmpout); 
                } 
        cout<<"Finished projection of"<<outputSt->size()<<" points in "<<time(NULL)-startTime<<" seconds"<<endl; 
        return(outputSt); 
        }
这样做是否正确,以便在方法完成后释放内存

再次编辑: 因此,我尝试了越来越远,最终我成功地将所有步骤放在一个函数中,如下所示:

void stereoTiffI(string infile, string outfile, int length)
    {
    //set up file input
    cout<<"Opening file: "<< infile<<endl;
    ifstream file;
    file.open(infile);
    string line;
    //skip first lines
    for (int i=0;i<9;i++)
        {
        getline(file,line);
        }
    //output cloud
    pcl::PointCloud<pcl::PointXYZ> cloud;
    getline(file,line);
    //indexes for string parsing, coordinates and starting Timer
    int i=0;
    int j=0;
    int k=0;
    float x=0;
    float y=0;
    float z=0;
    float intensity=0;
    float scalar=0;
    int startTime = time(NULL);
    pcl::PointXYZ tmp;
    //begin loop
    cout<<"Begin reading and projecting"<< infile<<endl;
    while (!file.eof())
        {

        getline(file,line);
        i=0;
        j=line.find(" ");
        x=atof(line.substr(i,j).c_str());
        i=line.find(" ",i)+1;
        j=line.find(" ",i)-i;
        y=atof(line.substr(i,j).c_str());
        i=line.find(" ",i)+1;
        j=line.find(" ",i)-i;
        z=atof(line.substr(i,j).c_str());
        //i=line.find(" ",i)+1;
        //j=line.find(" ",i)-i;
        //intensity=atof(line.substr(i,j).c_str());
        //leave out points below scanner height
        if (z>0)
            {
            //projection to a hemisphere with radius 1
            scalar=1/(sqrt(pow(x,2)+pow(y,2)+pow(z,2)));
            x=x*scalar;
            y=y*scalar;
            z=z*scalar;
            //stereographic projection
            x=x/(1.0+z);
            y=y/(1.0+z);
            z=0;
            tmp.x=x;
            tmp.y=y;
            tmp.z=z;
            //tmp.intensity=intensity;
            cloud.push_back(tmp);
            k++;
            if (k%1000000==0)cout<<k<<endl;
            }
        }
    cout<<"Finished producing projected cloud in: "<<time(NULL)-startTime<<" with "<<cloud.size()<<" points."<<endl;
void-stereodiffi(字符串填充、字符串输出文件、整数长度)
{
//设置文件输入

cout好的,我解决了这个问题。内存博士给了我一个正确的提示,给了我一个堆分配错误。通过谷歌搜索,我在Visual Studio中启用了大地址(属性->链接器->系统)
一切都像一个符咒。

好的,我解决了它。内存博士给了我一个正确的提示,给了我一个堆分配错误。通过谷歌搜索,我在Visual Studio中启用了大地址(属性->链接器->系统)
每件事都很有魅力。

我进一步研究了一下:如果我独立完成这些步骤,它有时会崩溃,但大多数情况下它都会崩溃,只是需要花费一些时间,因为我必须读/写大型点云(~500MB)一直如此。而且,它似乎在第一个或第二个循环中崩溃,每次运行都是不同的。崩溃似乎发生在1700万次或1100万次左右(每隔几次运行就不同)迭代?我在这里完全迷路了,因为它看起来太随机了。请帮我确定,所以我运行了几次,看看是否有任何模式,循环在每100个小时给出一个输出(更改了上面的代码)在15次运行中,它总是在第二个循环中崩溃,在17900000和11900000之间随机交替,它是这样的:179119179119179119179119179179119179179179179119179179179179179119179179179179179119179179179179179119179179179179179119179179179179119179179179179119179179179179119179179179179179119179179179179179179119179179179179179179119179179179179179179179119179179179179179179179119179179179179179179大点云(~500MB)一直在运行。此外,它似乎在第一个或第二个循环中崩溃,每次运行都不同。崩溃似乎发生在1700万次或1100万次左右(每几次运行都不同)迭代?我在这里完全迷路了,因为它看起来太随机了。请帮我确定,所以我运行了几次,看看是否有任何模式,循环在每100个小时给出一个输出(更改了上面的代码)在15次运行中,它总是在第二个循环中崩溃,在17900000和11900000之间随机交替,结果如下:179119179119179119179119179179119179
void stereoTiffI(string infile, string outfile, int length)
    {
    //set up file input
    cout<<"Opening file: "<< infile<<endl;
    ifstream file;
    file.open(infile);
    string line;
    //skip first lines
    for (int i=0;i<9;i++)
        {
        getline(file,line);
        }
    //output cloud
    pcl::PointCloud<pcl::PointXYZ> cloud;
    getline(file,line);
    //indexes for string parsing, coordinates and starting Timer
    int i=0;
    int j=0;
    int k=0;
    float x=0;
    float y=0;
    float z=0;
    float intensity=0;
    float scalar=0;
    int startTime = time(NULL);
    pcl::PointXYZ tmp;
    //begin loop
    cout<<"Begin reading and projecting"<< infile<<endl;
    while (!file.eof())
        {

        getline(file,line);
        i=0;
        j=line.find(" ");
        x=atof(line.substr(i,j).c_str());
        i=line.find(" ",i)+1;
        j=line.find(" ",i)-i;
        y=atof(line.substr(i,j).c_str());
        i=line.find(" ",i)+1;
        j=line.find(" ",i)-i;
        z=atof(line.substr(i,j).c_str());
        //i=line.find(" ",i)+1;
        //j=line.find(" ",i)-i;
        //intensity=atof(line.substr(i,j).c_str());
        //leave out points below scanner height
        if (z>0)
            {
            //projection to a hemisphere with radius 1
            scalar=1/(sqrt(pow(x,2)+pow(y,2)+pow(z,2)));
            x=x*scalar;
            y=y*scalar;
            z=z*scalar;
            //stereographic projection
            x=x/(1.0+z);
            y=y/(1.0+z);
            z=0;
            tmp.x=x;
            tmp.y=y;
            tmp.z=z;
            //tmp.intensity=intensity;
            cloud.push_back(tmp);
            k++;
            if (k%1000000==0)cout<<k<<endl;
            }
        }
    cout<<"Finished producing projected cloud in: "<<time(NULL)-startTime<<" with "<<cloud.size()<<" points."<<endl;