代码运行结束报段错误 (核心已转储)

qiguijiao7013 2017-10-19 09:09:59
在学习pcl库时参考教程写了个点云配准的代码,主程序如下:
int main(int argc, char** argv){

std::vector<PCD, Eigen::aligned_allocator<PCD> > data;
loadData(argc, argv, data);

if(data.empty()){
PCL_ERROR("Syntax is: %s <source.pcd> <target.pcd>
  • ", argv
  • [0]);
    PCL_ERROR("
  • - multiple files can be added. The registration results of
  • (i,i+1) will be registered against (i+2). etc");
    return -1;
    }

    PCL_INFO("Loaded %d datasets.", (int)data.size());

    PCLVisualizer = new pcl::visualization::PCLVisualizer(argc, argv, "Pairwise Incremental Registration example");

    PCLVisualizer->createViewPort(0.0, 0, 0.5, 1.0, vp_1);
    PCLVisualizer->createViewPort(0.5, 0, 1.0, 1.0, vp_2);

    pcl::PointCloud<pcl::PointXYZ>::Ptr result(new pcl::PointCloud<pcl::PointXYZ>), source, target;
    Eigen::Matrix4f GlobalTransform = Eigen::Matrix4f::Identity(), pairTransform;

    for(size_t i=1; i<data.size();i++){

    source = data[i-1].cloud;
    target = data[i].cloud;

    showCloudsLeft(source, target);

    pcl::PointCloud<pcl::PointXYZ>::Ptr temp(new pcl::PointCloud<pcl::PointXYZ>);
    PCL_INFO("Aligning %s (%d) with %s (%d).\n", data[i-1].f_name.c_str(), source->points.size(), data[i].f_name.c_str(), target->points.size());

    pairAlign(source, target, temp, pairTransform, true);
    std::cout << "pair registration finished" << endl; // 这一句无法输出说明上一步函数pairAlign存在问题

    pcl::transformPointCloud(*temp, *result, GlobalTransform);

    GlobalTransform = GlobalTransform * pairTransform;

    std::stringstream ss;
    ss << i << ".pcd";
    pcl::io::savePCDFile(ss.str(), *result, true);
    }
    }


函数写在另一个cpp文件中,头文件已在主程序中声明了。
void pairAlign (const pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_src, const pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_tgt, pcl::PointCloud<pcl::PointXYZ>::Ptr output, Eigen::Matrix4f &final_transform, bool downsample = false){
//
// Downsample for consistency and speed
// \note enable this for large datasets
pcl::PointCloud<pcl::PointXYZ>::Ptr src(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr tgt(new pcl::PointCloud<pcl::PointXYZ>);
pcl::VoxelGrid<pcl::PointXYZ> grid;

if(downsample){
grid.setLeafSize(0.1, 0.1, 0.1);
grid.setInputCloud(cloud_src);
grid.filter(*src);

grid.setInputCloud(cloud_tgt);
grid.filter(*tgt);
}else{
src = cloud_src;
tgt = cloud_tgt;
}

pcl::PointCloud<pcl::PointNormal>::Ptr points_with_normals_src(new pcl::PointCloud<pcl::PointNormal>);
pcl::PointCloud<pcl::PointNormal>::Ptr points_with_normals_tgt(new pcl::PointCloud<pcl::PointNormal>);

pcl::NormalEstimation<pcl::PointXYZ,pcl::PointNormal> norm_est; //点云法线估计对象
pcl::search::KdTree<pcl::PointXYZ>::Ptr kdtree(new pcl::search::KdTree<pcl::PointXYZ>());

norm_est.setSearchMethod(kdtree);
norm_est.setKSearch(30);

norm_est.setInputCloud(src);
norm_est.compute(*points_with_normals_src);
pcl::copyPointCloud(*src, *points_with_normals_src);

norm_est.setInputCloud(tgt);
norm_est.compute(*points_with_normals_tgt);
pcl::copyPointCloud(*tgt, *points_with_normals_tgt);

// 这是一个自己创建的点表达类
MyPointRepresentation point_representation;
float alpha[4] = {1.0, 1.0, 1.0, 1.0};
point_representation.setRescaleValues(alpha);

pcl::IterativeClosestPointNonLinear<pcl::PointNormal, pcl::PointNormal> reg;

reg.setTransformationEpsilon(1e-6);
reg.setMaxCorrespondenceDistance(0.05);
reg.setPointRepresentation(boost::make_shared<const MyPointRepresentation>(point_representation));

reg.setInputSource(points_with_normals_src);
reg.setInputTarget(points_with_normals_tgt);

Eigen::Matrix4f Ti = Eigen::Matrix4f::Identity(), prev, targetToSource;
pcl::PointCloud<pcl::PointNormal>::Ptr reg_result = points_with_normals_src;
reg.setMaximumIterations(10);
for(int i=0; i<6; i++){
PCL_INFO("Iteration Nr. %d.\n", i);

points_with_normals_src = reg_result;

// Estimate
reg.setInputSource(points_with_normals_src);
reg.align(*reg_result);
Ti = reg.getFinalTransformation() * Ti;

if( fabs((reg.getLastIncrementalTransformation()-prev).sum())<reg.getTransformationEpsilon() ){
reg.setMaxCorrespondenceDistance(reg.getMaxCorrespondenceDistance()-0.01);
}

prev = reg.getLastIncrementalTransformation();

// 显示函数,函数的定义也保存在这个cpp文件中,声明保存在头文件中
showCloudsRight(points_with_normals_tgt, points_with_normals_src);
}

std::cout << "has converged:" << reg.hasConverged() << " score: " << reg.getFitnessScore() << std::endl;
targetToSource = Ti.inverse();

pcl::transformPointCloud(*cloud_tgt, *output, targetToSource);

// display
PCLVisualizer->removePointCloud ("source");
PCLVisualizer->removePointCloud ("target");
PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_tgt_h (output, 0, 255, 0);
PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_src_h (cloud_src, 255, 0, 0);
PCLVisualizer->addPointCloud (output, cloud_tgt_h, "target", vp_2);
PCLVisualizer->addPointCloud (cloud_src, cloud_src_h, "source", vp_2);
PCL_INFO ("Press q to continue the registration.\n");
PCLVisualizer->spin();
PCLVisualizer->removePointCloud ("source");
PCLVisualizer->removePointCloud ("target");

*output += *cloud_src;
std::cout << output->points.size() << " points have been merged into the source point cloud." << endl;
final_transform = targetToSource;
std::cout << "the transform matrix is:\n" << final_transform << endl; // 这一句可以运行,不报错。
// 这就很奇怪了,程序运行到了最后出现错误,第一次遇到这种问题,检查了函数的声明定义,应该没声没问题啊,难道时pcl点云指针在这儿释放了不能返回?
}


程序运行到函数末尾都可以正常运行,但是运行结束后报错。第一次遇到这种问题,检查了函数的声明定义,应该没声没问题啊,难道时pcl点云指针在这儿释放了不能返回?

我把这个函数整个copy到了主函数的cpp文档中,就可以正常运行。求问一下大神,这到底是为什么?是pcl指针内存问题吗
...全文
789 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
好脾气先生 2019-12-16
  • 打赏
  • 举报
回复
大佬解决了么?我现在也遇到了这个问题,在LINUX下。排查发现是只有在PointCloud2的类型时才能读取成功。PointCloud条件下就会出现段错误提示。不知道楼主有没有好的解决办法
赵4老师 2017-10-20
  • 打赏
  • 举报
回复
Windows: 崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。 Linux: 进程意外退出会在当前目录下产生‘core’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
qiguijiao7013 2017-10-19
  • 打赏
  • 举报
回复
自己顶一下!大神们求解答!

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧