Eigen库使用过程出现的一些编译warning、运行崩溃问题记录

一、编译warning

1.1 编译环境VisualStudio2017企业版15.9.9

类中含有Eigen库对象,使用make_shared动态分配指向该类类型的智能指针时,编译报如下错误:

#include <memory>
#include <iostream>
#include <eigen3/Eigen/Dense>

struct Object
{
	Eigen::Matrix4d pMatrix;

	Object()
	{
		pMatrix << 0.1, 0, 0, 0,
			0, 0.1, 0, 0,
			0, 0, 0.1, 0,
			0, 0, 0, 0.1;
	}
};

int main(void)
{
	using namespace std;

	shared_ptr<Object> shPtr11 = make_shared<Object>(); // 使用eigen库时编译报错
	shared_ptr<int> shPtr22 = make_shared<int>();
	
	return 0;
}

error C2338: You've instantiated std::aligned_storage<Len, Align> with an extended alignment (in other words, Align > alignof(max_align_t)). Before VS 2017 15.8, the member type would non-conformingly have an alignment of only alignof(max_align_t). VS 2017 15.8 was fixed to handle this correctly, but the fix inherently changes layout and breaks binary compatibility (*only* for uses of aligned_storage with extended alignments). Please define either (1) _ENABLE_EXTENDED_ALIGNED_STORAGE to acknowledge that you understand this message and that you actually want a type with an extended alignment, or (2) _DISABLE_EXTENDED_ALIGNED_STORAGE to silence this message and get the old non-conformant behavior.(双击可定位到系统头文件<type_traits>)


大概意思就是:VS2017 15.8版本修复了老版本有关对齐存储部分缺陷,但修复本身也有缺陷。如果不想编译时报这个问题,就在预编译时定义一个宏 _ENABLE_EXTENDED_ALIGNED_STORAGE 或者 _DISABLE_EXTENDED_ALIGNED_STORAGE(笔者的理解是按照修复后的逻辑处理就定义带enable那个,按照老版本的逻辑处理就定义带disable那个)

由于笔者建的是一个Empty Project,无预编译头文件,所以进入下图设置后就未再报错:

1.2 编译环境Ubuntu x86_64

类中含有Eigen库对象,使用new动态分配指向该类类型的指针时,编译出现下图所示警告:

 解决该编译警告有图示两种手段:

(1)新增编译选项 “-faligned-new”,推荐,原因见下文。

(2)在类中调用宏EIGEN_MAKE_ALIGNED_OPERATOR_NEW,这是Eigen通过重载new和delete方法(/eigen3/Eigen/src/Core/util/Memory.h)来实现aligned_allocator。

二、运行崩溃

类中含有Eigen库对象,使用make_shared动态分配指向该类类型的智能指针时,编译无错无警告,但运行崩溃。使用new动态分配指向该类类型的指针时,运行正常。(编译环境Ubuntu x86_64)

猜测还是因为内存未对齐导致。虽然在Frame类中调用了宏EIGEN_MAKE_ALIGNED_OPERATOR_NEW,而在ObstacleMultiSensorFusion类中是否需要调用此宏,经验证无作用。后来幡然想到该宏是Eigen针对new和delete两运算符的重载,所以尝试新增编译选项 “-faligned-new” ,发现使用make_shared也可以正常运行。

  

待续……   

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值