Search code examples
memoryeigenpoint-cloud-library

PCL 1.8.1 crash on cloud delete after StatisticalOutliersRemoval


I have an application which is build using several DLL files. I'm trying to perform PCL's statistical outliers removal using the following code:

    PointCloudWithRGBNormalsPtr pclCloud(new PointCloudWithRGBNormals());

    ConvertPointCloudToPCL(in_out_cloud /*my own structure which includes xyz, rgb, nx ny nz*/, *pclCloud);

    pcl::StatisticalOutlierRemoval<PointXYZRGBNormal> sor;
    sor.setInputCloud(pclCloud);
    sor.setMeanK(10);
    sor.setStddevMulThresh(1.0);
    sor.filter(*pclCloud);

ConvertPointCloudToPCL:

static void ConvertPointCloudToPCL(const std::vector<Cloud3DrgbN> &in, PointCloudWithRGBNormals &output)
{
    for (auto it = in.begin(); it != in.end(); it++)
    {
        const Cloud3DrgbN &p3d = *it;;
        PointXYZRGBNormal p;
        p.x = p3d.x;
        p.y = p3d.y;
        p.z = p3d.z;
        p.normal_x = p3d.nX;
        p.normal_y = p3d.nY;
        p.normal_z = p3d.nZ;
        p.r = p3d.r;
        p.g = p3d.g;
        p.b = p3d.b;
        output.push_back(p);
    }
}

For some reason, if I call this function from 1 of my dlls it works as it should. However, there's 1 dll that if I call it from it, when pclCloud goes out of scope, I'm getting an exception from Eigen's Memory.h file at the handmade_aligned_free function

I'm using Windows 10 64-bit, pcl 1.8.1 and Eigen 3.3 (tried 3.3.4, same thing)

Update:

After further digging, I've found that EIGEN_MALLOC_ALREADY_ALIGNED was set to 0 because I'm using AVX2 in my "problematic" DLL. I'm still not sure though why using Eigen's "handmade" aligned malloc/free causes this crash.

There seems to be a known issue (see this) with Eigen, PCL & AVX


Solution

  • Well I found the problem and how to solve it.

    It seems that the DLLs that comes with the "All in 1 installer" for windows weren't compiled with AVX/AVX2 support.

    When linking these libraries with my own DLLs, the ones that compiled using the AVX, this mismatch caused Eigen to use the different types of allocations and freeing of memory causing the crash.

    I compiled PCL from source using AVX2 and linked these library and everything works.

    It's worth mentioning that the DLL that worked before now has issues since it doesn't have AVX and PCL now do.