Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RANSAC Example Segmentation Fault on PCL 1.11 #4337

Closed
mmartin4972 opened this issue Aug 19, 2020 · 24 comments
Closed

RANSAC Example Segmentation Fault on PCL 1.11 #4337

mmartin4972 opened this issue Aug 19, 2020 · 24 comments
Labels
kind: bug Type of issue needs: author reply Specify why not closed/merged yet

Comments

@mmartin4972
Copy link

I''m using Ubuntu 18, with CUDA 10.0, and PCL 1.11. My code compiles however whenever I try to run the SAC Segmentation my code segfaults when it segments the cloud. I'm using the same code as from the example in the docs and it continues to segfault. Any help would be greatly appreciated.


  pcl::PointCloud::Ptr cloud(new pcl::PointCloud);

  // Fill in the cloud data
  cloud->width  = 15;
  cloud->height = 1;
  cloud->points.resize (cloud->width * cloud->height);

  // Generate the data
  for (auto& point: *cloud)
  {
    point.x = 1024 * rand () / (RAND_MAX + 1.0f);
    point.y = 1024 * rand () / (RAND_MAX + 1.0f);
    point.z = 1.0;
  }

  // Set a few outliers
  (*cloud)[0].z = 2.0;
  (*cloud)[3].z = -2.0;
  (*cloud)[6].z = 4.0;

    //Creates instance of RANSAC Algorithm
    pcl::SACSegmentation seg;
    pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
    pcl::PointIndices::Ptr inliers(new pcl::PointIndices());
    seg.setModelType(pcl::SACMODEL_PLANE);
    seg.setMethodType(pcl::SAC_RANSAC);
    seg.setMaxIterations(400);
    seg.setDistanceThreshold(60); //Distance in mm away from actual plane a point can be to be inlier
    seg.setInputCloud(cloud);
    seg.segment(*inliers, *coefficients);
@mmartin4972 mmartin4972 added the status: triage Labels incomplete label Aug 19, 2020
@mvieth
Copy link
Member

mvieth commented Aug 19, 2020

Could you please run you program once with gdb, once with valgrind? That should tell us where the segfault happens.

@kunaltyagi kunaltyagi added kind: bug Type of issue needs: author reply Specify why not closed/merged yet and removed status: triage Labels incomplete labels Aug 19, 2020
@mvieth
Copy link
Member

mvieth commented Aug 19, 2020

@mmartin4972 BTW your code does not compile (for me) because the template parameters for cloud and seg are missing.

@mmartin4972
Copy link
Author

Thanks for the quick response. When I ran valgrind with it the program did not segfault. I included the output from when I ran it. I also ran gdb and the program made it up to line 99 of sac_segmentation.hpp and then segfaulted on the next step. I think that is when it runs "getInliers()". It went into this file when 'seg.segment(*inliers, *coefficients);' was run. Here is the gdb output:

`Thread 1 "onboard_cv" hit Breakpoint 3, pcl::SACSegmentation<pcl::PointXYZ>::segment (this=0x7fffffffd800, inliers=..., model_coefficients=...)
    at /usr/local/pcl-pcl-1.11.1/segmentation/include/pcl/segmentation/impl/sac_segmentation.hpp:82
82	  if (!initCompute ()) 
(gdb) l
77	pcl::SACSegmentation<PointT>::segment (PointIndices &inliers, ModelCoefficients &model_coefficients)
78	{
79	  // Copy the header information
80	  inliers.header = model_coefficients.header = input_->header;
81	
82	  if (!initCompute ()) 
83	  {
84	    inliers.indices.clear (); model_coefficients.values.clear ();
85	    return;
86	  }
(gdb) next
89	  if (!initSACModel (model_type_))
(gdb) next
97	  initSAC (method_type_);
(gdb) next
99	  if (!sac_->computeModel (0))
(gdb) next

Thread 1 "onboard_cv" received signal SIGSEGV, Segmentation fault.
__GI___libc_free (mem=0x31) at malloc.c:3103
3103	malloc.c: No such file or directory.
(gdb) l
3098	in malloc.c
(gdb) 
`

@mmartin4972
Copy link
Author

Could you also elaborate on what you mean by template parameters for cloud and seg? I thought I declared those properly.

@mmartin4972 mmartin4972 changed the title RANSAC Example Segmentation Faults on PCL 1.11 RANSAC Example Segmentation Fault on PCL 1.11 Aug 20, 2020
@mvieth
Copy link
Member

mvieth commented Aug 20, 2020

Could you also elaborate on what you mean by template parameters for cloud and seg? I thought I declared those properly.

You wrote pcl::PointCloud::Ptr cloud(new pcl::PointCloud); but it should be pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);. Likewise pcl::SACSegmentation<pcl::PointXYZ> seg; instead of pcl::SACSegmentation seg;. Do you not get compilation errors like error: ‘template<class PointT> class pcl::PointCloud’ used without template arguments?

I also ran gdb and the program made it up to line 99 of sac_segmentation.hpp and then segfaulted on the next step. I think that is when it runs "getInliers()".

No, the segfault seems to happen at line 99, in computeModel. The valgrind output is helpful too: It reports invalid reads, which can result in a segfault. They all involve Eigen's Matrices, so that might be where the problem lies. What Eigen version do you use? Installed via apt or some other way? Can you try to run some other program that uses an Eigen::Matrix with valgrind?

Also: what pcl version do you use exactly? 1.11.0 or 1.11.1? Compiled from source?

@mmartin4972
Copy link
Author

On the code on my computer it does have the template arguments and compiles properly. I must have copied the code over incorrectly some how.

I use PCL 1.11.1 compiled from source. I use Eigen 3.3.7 compiled from source.

I didn't have another program that uses Eigen, so I copied the sample program from here and ran it with Valgrind and got the following output:

==17495== Memcheck, a memory error detector
==17495== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==17495== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==17495== Command: ./test
==17495== 
  3  -1
2.5 1.5
==17495== 
==17495== HEAP SUMMARY:
==17495==     in use at exit: 0 bytes in 0 blocks
==17495==   total heap usage: 3 allocs, 3 frees, 73,760 bytes allocated
==17495== 
==17495== All heap blocks were freed -- no leaks are possible
==17495== 
==17495== For counts of detected and suppressed errors, rerun with: -v
==17495== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

@mvieth
Copy link
Member

mvieth commented Aug 21, 2020

Could you try your program with MSAC instead seg.setMethodType(pcl::SAC_MSAC);, and maybe also with LMEDS, both with valgrind?

Please try also this example with valgrind.

@SergioRAgostinho
Copy link
Member

SergioRAgostinho commented Aug 21, 2020

Did you compile pcl with C++17 support by any chance?

@mmartin4972
Copy link
Author

Hi, the MSAC and LMEDS both segfaulted. Here are the valgrind files for MSAC and LMEDS. The example did not segfault and here is its valgrind output. I compile pcl with C++14 support.

@SergioRAgostinho
Copy link
Member

Can you print the compiler flags that were used to compile pcl and the compiler flags which are being passed to your downstream application. Call

$ VERBOSE=1 make

when building both pcl and your application.

@tkuraku
Copy link

tkuraku commented Aug 21, 2020

I had the same problem with PCL 1.11 compiled from source on Ubuntu 18.04. I used the sample unchanged from the website and I get a segfault.

@tkuraku
Copy link

tkuraku commented Aug 21, 2020

I just installed PCL 1.11.1 on windows from here: https://github.com/PointCloudLibrary/pcl/releases/tag/pcl-1.11.1 and tried the example here: https://pcl.readthedocs.io/projects/tutorials/en/latest/alignment_prerejective.html#alignment-prerejective

I don't get a segfault like on ubuntu, but it doesn't align the clouds.

image

> Loading point clouds...
Failed to find match for field 'curvature'.
Failed to find match for field 'normal_x'.
Failed to find match for field 'normal_y'.
Failed to find match for field 'normal_z'.
Failed to find match for field 'curvature'.
> Downsampling...
> Estimating scene normals...
> Estimating features...
> Starting alignment...
Alignment took 1219.4ms.

    |  0.871  0.194  0.451 |
R = | -0.479  0.135  0.867 |
    |  0.107 -0.972  0.210 |

t = < 0.238, 0.657, 0.730 >

Inliers: 1195/3432

@mmartin4972
Copy link
Author

I compile my code with meson, and it doesn't like the VERBOSE=1 argument. I tried compiling the -g3, but no extra output was provided. Do you have any idea as to why my code continues segfaulting?

@SergioRAgostinho
Copy link
Member

I need to see the actual call to the compiler your build system does when building pcl and when building your application which depends on pcl.

@mvieth
Copy link
Member

mvieth commented Aug 25, 2020

I tried to reproduce your error (Ubuntu 18.04.5, Eigen 3.3.7 self installed, pcl 1.11.1 compiled from source), but was unable to do so. I also found this issue that seems to be related.

@mmartin4972
Copy link
Author

When you tried to reproduce the error did you build with GPU and install CUDA? I feel like that might be part of the problem for me, since when using PCL 1.8 I didn't build the gpu library, but now I am.

@mmartin4972
Copy link
Author

I just came across closed issue #2147 and found the solution to my problem was that I needed to compile with the '-mavx' flag. All is working properly now. Thank you for all of your help!

@goksanisil23
Copy link

I had the same problem as @mmartin4972, on Ubuntu 20.04 with PCL 1.10 and Eigen 3.4

The "-march=native" flag was causing the issue and removing it solves the problem.

@lcuau
Copy link

lcuau commented Apr 9, 2024

I have the same problem avec I used the options in my cmakelist
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.2")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpmath=sse")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx ")
but the function ransac.computeModel() still segfault .... Please help

@mvieth
Copy link
Member

mvieth commented Apr 9, 2024

@lcuau Which operating system? Which PCL version? How did you install PCL? Can you post a backtrace of the segfault (with gdb or valgrind)?

@lcuau
Copy link

lcuau commented Apr 9, 2024

Sorry, I use Ubuntu 20.04.6 LTS, PCL VERSION 1.13.1.99, I installed pcl from source (https://github.com/PointCloudLibrary/pcl/releases/tag/pcl-1.13.1), here is the backtrace from gdb

Thread 1 "normal_test" received signal SIGSEGV, Segmentation fault.
__GI___libc_free (mem=0x31) at malloc.c:3102
3102 malloc.c: Aucun fichier ou dossier de ce type.
(gdb) bt
#0 __GI___libc_free (mem=0x31) at malloc.c:3102
#1 0x00005555555a6b95 in pcl::SampleConsensuspcl::PointXYZRGB::~SampleConsensus() ()
#2 0x00005555555a206a in norm::NManager::compute_n_v2(float, int&) ()
#3 0x00005555555757f3 in main ()
(gdb)

end my code is simply

pcl::SampleConsensusModelPlanepcl::PointXYZRGB::Ptr model_p (new pcl::SampleConsensusModelPlanepcl::PointXYZRGB (cloud));
pcl::RandomSampleConsensuspcl::PointXYZRGB ransac (model_p);
ransac.setDistanceThreshold (.01);
ransac.computeModel();

(with cloud a non empty cloud get from 3D camera)

@mvieth
Copy link
Member

mvieth commented Apr 9, 2024

@lcuau One possible reason for this is that PCL and your own project are compiled with different C++ standards. Assuming PCL is compiled as C++14 (the default), make sure your own project is also compiled as C++14. Another reason is that the architecture options (sse, avx, march=native) are different when compiling your own project vs when compiling PCL. However, usually CMake should take care to use the same options that were used when PCL was compiled, so I would rather recommend against changing those options manually.

@lcuau
Copy link

lcuau commented Apr 9, 2024

Alright. Right now I am trying to re-compile pcl and I checked the CMakeList to be sure of the options used. When it will be done I will try to compile my own code. Is it enough to build my code with "target_compile_options(name PUBLIC ${PCL_COMPILE_OPTIONS})" ? Or should I add by hand all the option that I see in the pcl CMakeList in my own CMakeList ?

@lcuau
Copy link

lcuau commented Apr 9, 2024

OK I am not sure of what did the job but I added the following lines in my CmakeList and it worked ! Thank you a lot

set(CMAKE_CXX_STANDARD 14 CACHE STRING "The target C++ standard. PCL requires C++14 or higher.")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lboost_system")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.2")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpmath=sse")
add_definitions(-DEIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT
-DEIGEN_DONT_VECTORIZE)
add_definitions(-DEIGEN_DONT_ALIGN)
add_definitions(${PCL_DEFINITIONS})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug Type of issue needs: author reply Specify why not closed/merged yet
Projects
None yet
Development

No branches or pull requests

7 participants