今天编译完成lidar_project后运行程序,出现下面的bug提示
back_end_node: /usr/include/eigen3/Eigen/src/Core/DenseStorage.h:128: Eigen::internal::plain_array<T, Size, MatrixOrArrayOptions, 32>::plain_array() [with T = float; int Size = 16; int MatrixOrArrayOptions = 0]: Assertion `(internal::UIntPtr(eigen_unaligned_array_assert_workaround_gcc47(array)) & (31)) == 0 && "this assertion is explained here: " "http://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html" " **** READ THIS WEB PAGE !!! ****"' failed.
之前也见过Eigen的bug,基本是因为内存管理的问题,使用容器里面是eigen类型的变量,需要在变量声明时主动声明内存,类似于下面这段,这次这个问题给了个提示的网站,原来开发者也知道有这样的问题,提前做了预案,一共四种情况,我们一项一项看看。写在前面,文章里说用c++17就可以避免这样的问题。
解决方案:
调整了几天,即使按照情况1来改也没有解决这个问题,最后看到个帖子关于使用share_ptr和Eigen的,终于解决了这个问题,具体改动如下:
// std::shared_ptr<BackEndFlow> back_end_flow_ptr = std::make_shared<BackEndFlow>(nh);
std::shared_ptr<BackEndFlow> back_end_flow_ptr = std::shared_ptr<BackEndFlow>(new BackEndFlow(nh));
有文章里说是因为make_shared()这种方式还是忽略了重载中的operater new,所以需要显示调用类重载的operater new。另外,因为一个node程序会依赖很多个子程序,子程序中使用eigen和智能指针的都需要进行改动。
参考的网址就是提示中的
Eigen: Explanation of the assertion on unaligned arrays
std::vector<Eigen::Vector4f,Eigen::aligned_allocator<Eigen::Vector4f> >
1、结构体或者类内有Eigen的对象
如下所示,类内有vector4d的Eigen类型对象。
class Foo
{
//...
Eigen::Vector4d v;
//...
};
//...
Foo *foo = new Foo;
对于这种情况,需要在类内的public中写个宏,算是重载了new(),如下
class Foo
{
...
Eigen::Vector4d v;
...
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
Foo *foo = new Foo;
2、 使用了标准的容器STL Containers or manual memory allocation
These issues arise only with fixed-size vectorizable Eigen types and structures having such Eigen objects as member. For other Eigen types, such as Vector3f or MatrixXd, no special care is needed when using STL containers.
使用了像vector这样的容器,像下面这样
std::vector<Eigen::Matrix2d> my_vector;
struct my_class { ... Eigen::Matrix2d m; ... };
std::map<int, my_class> my_map;
改成
#include<Eigen/StdVector>
/* ... */
std::vector<Eigen::Vector4f,Eigen::aligned_allocator<Eigen::Vector4f> >
3、函数直接使用Eigen对象传值
void func(Eigen::Vector4d v);
上面这样的改成引用
void my_function(const Eigen::Vector2d& v);
如果是类或者结构体类内有Eigen变量,那么需要用该类时需要如下:
struct Foo
{
Eigen::Vector2d v;
};
//void my_function(Foo v);
void my_function(const Foo& v);
4、for instance GCC on Windows
Eigen: Explanation of the assertion on unaligned arrays
参考链接:
Eigen 与std::make_shared,std::unique_ptr 搭配使用的采坑记录 - 知乎
c++11 - Eigen::aligned_alocator & std::shared_ptr - Stack Overflow
std::make_shared fails to fulfill structure aliment (#1049) · Issues · libeigen / eigen · GitLab