You need to enable JavaScript to run this app. VS2017使用LASlib重采样点云出现__acrt_first_block断言失败求助
优惠活动
大模型
产品
解决方案
定价
更多

VS2017使用LASlib重采样点云出现__acrt_first_block断言失败求助

问题基本信息
  • 开发环境:VS2017,基于LASlib、PCL实现LAS点云读取、VoxelGrid降采样、重写LAS文件的处理流程
  • 报错表现:Debug模式运行时触发Debug Assertion Failed!,弹窗提示错误表达式为__acrt_first_block==heap.cpp
  • 已尝试操作:已在项目属性页将运行时库调整为「多线程调试DLL(/MDd)」,报错未消除
  • 报错截图(替代文本:enter image description here)
实现代码
#include <fstream>
#include <liblas/liblas.hpp>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/keypoints/uniform_sampling.h>
#include <pcl/filters/voxel_grid.h>

int main()
{
    //打开las文件
    std::ifstream ifs("E:/temp/in/Tile_1321222320002302112.las", std::ios::in | std::ios::binary);
    liblas::ReaderFactory f;
    liblas::Reader reader = f.CreateWithStream(ifs);

    //读取las头信息
    liblas::Header const& header = reader.GetHeader();
    int nbPoints = header.GetPointRecordsCount();

    //转换为pcl格式
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr in_cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
    while (reader.ReadNextPoint())
    {

        //坐标信息
        double x = reader.GetPoint().GetX();
        double y = reader.GetPoint().GetY();
        double z = reader.GetPoint().GetZ();

        //颜色信息
        uint16_t r1 = reader.GetPoint().GetColor().GetRed();
        uint16_t g1 = reader.GetPoint().GetColor().GetGreen();
        uint16_t b1 = reader.GetPoint().GetColor().GetBlue();
        uint32_t r2 = ceil(((float)r1 / 65536)*(float)256);
        uint32_t g2 = ceil(((float)g1 / 65536)*(float)256);
        uint32_t b2 = ceil(((float)b1 / 65536)*(float)256);
        uint32_t rgb = ((int)r2 << 16 | (int)g2 << 8 | (int)b2);
        pcl::PointXYZRGB thept;
        thept.x = x;
        thept.y = y;
        thept.z = z;
        thept.rgb = rgb;
        in_cloud->push_back(thept);

    }

    //采样
    //pcl::UniformSampling<pcl::PointXYZRGB> filter;
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr filteredCloud(new pcl::PointCloud<pcl::PointXYZRGB>);
    
    //均匀
    //filter.setInputCloud(in_cloud);
    //filter.setRadiusSearch(0.1f);
    //filter.filter(*filteredCloud);

    pcl::VoxelGrid<pcl::PointXYZRGB> filter;
    filter.setInputCloud(in_cloud);
    filter.setLeafSize(0.1f,0.1f,0.1f);
    filter.filter(*filteredCloud);
    int out_p_n = filteredCloud->size();

    //写入las文件
    std::string save_path = "E:/temp/out/";
    std::string name = "Tile_1321222320002302112.las";

    double minPt[3] = { 9999999, 9999999, 9999999 };
    double maxPt[3] = { 0, 0, 0 };

    std::ofstream ofs = std::ofstream(save_path + name, std::ios::out | std::ios::binary);
    //设置文件头、点数、格式、缩放因子、偏移量
    liblas::Header f_header;
    f_header.SetVersionMajor(1);
    f_header.SetVersionMinor(2);
    f_header.SetMin(minPt[0], minPt[1], minPt[2]);
    f_header.SetMax(maxPt[0], maxPt[1], maxPt[2]);
    f_header.SetDataFormatId(liblas::PointFormatName::ePointFormat3);
    f_header.SetOffset(0, 0, 0);
    f_header.SetScale(0.001, 0.001, 0.001);
    f_header.SetPointRecordsCount(out_p_n);
    liblas::Writer writer(ofs,f_header);
    liblas::Point point(&f_header);

    for (size_t i = 0; i < filteredCloud->size(); ++i)
    {
        double x = filteredCloud->points[i].x;
        double y = filteredCloud->points[i].y;
        double z = filteredCloud->points[i].z;
        point.SetX(x);
        point.SetY(y);
        point.SetZ(z);
        uint32_t r = (uint32_t)filteredCloud->points[i].r;
        uint32_t g = (uint32_t)filteredCloud->points[i].g;
        uint32_t b = (uint32_t)filteredCloud->points[i].b;
        liblas::Color pointColor(r, g, b);
        point.SetColor(pointColor);
        writer.WritePoint(point);
    }

    writer.SetHeader(f_header);
    ofs.flush();
    ofs.close();

    return EXIT_SUCCESS;

}
问题定位
  • 核心根因:跨模块CRT堆不匹配
    单独调整当前项目的运行时库为/MDd不足以解决问题。VS的Debug堆校验机制要求所有动态分配、释放内存的模块(包括依赖的LASlib静态/动态库、PCL库)必须使用完全一致的CRT运行时版本。如果任意一个依赖库是用/MTd(多线程静态调试)模式编译的,跨模块释放内存时就会触发__acrt_first_block==heap.cpp的断言失败。
  • 代码层面的触发隐患
    • 未校验输入文件流打开状态,文件路径无效时会直接导致Reader内部指针空引用,触发堆越界
    • 初始化的minPt、maxPt数组未做实际点云范围遍历赋值,传入Header的极值不符合真实数据范围
    • 写完所有点之后才调用writer.SetHeader(f_header),会触发Writer内部重复的元数据缓存块内存释放,和CRT堆校验机制冲突
修复方案
  • 统一全链路运行时库配置
    确认当前项目、LASlib所有Debug依赖库、PCL Debug库的编译选项完全统一为「多线程调试DLL(/MDd)」,禁止混用/MTd、/MDd不同模式编译的库文件
  • 修正代码逻辑
    • 文件流打开后第一时间校验状态:
      std::ifstream ifs("xxx.las", std::ios::in | std::ios::binary);
      if (!ifs.is_open()) return EXIT_FAILURE;
      
    • 在读取点云的循环中逐点计算真实的坐标min/max值,替换初始的无效极值
    • LAS Writer的Header更新时机调整为:所有点写入完成后,调用Writer提供的UpdateHeader()方法更新元数据,去掉写完点之后重复调用SetHeader的逻辑
    • 颜色值适配LAS 1.2格式的16位通道要求,将从PCL取出的8位通道值左移8位转为16位值后再传入liblas::Color构造函数,避免通道值越界

内容的提问来源于stack exchange,提问作者paobukuaideluozi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.11 19:36:05