Eigen 3.3.4内存对齐失效求助:曾正常运行现频繁崩溃
EIGEN_MAKE_ALIGNED_OPERATOR_NEW I’ve run into this exact alignment headache with Eigen on newer Fedora releases, so let’s break down the likely causes and actionable fixes based on your scenario:
1. Your Container Isn’t Using Eigen’s Aligned Allocator
This is the most common culprit when EIGEN_MAKE_ALIGNED_OPERATOR_NEW doesn’t resolve the issue. If you’re storing NDTCell instances in a standard container like std::vector, the default allocator doesn’t guarantee the 16-byte alignment Eigen requires for fixed-size vector/matrix members.
Fix:
Swap in Eigen’s aligned allocator for any dynamic containers holding your class:
#include <Eigen/StdVector> // Replace your standard vector definition with this std::vector<NDTCell, Eigen::aligned_allocator<NDTCell>> cell_container;
This ensures every NDTCell instance allocated by the vector meets Eigen’s strict alignment needs.
2. Inheritance or Composition Is Breaking Alignment
If NDTCell inherits from another class that doesn’t handle alignment properly, or contains a non-aligned member, your EIGEN_MAKE_ALIGNED_OPERATOR_NEW macro won’t be enough to fix the chain.
Fixes:
- If
NDTCellhas a parent class, addEIGEN_MAKE_ALIGNED_OPERATOR_NEWto the parent class as well. - Force alignment for the entire
NDTCellclass using theEIGEN_ALIGN16macro:class EIGEN_ALIGN16 NDTCell { // Your fixed-size Eigen members here (e.g., Eigen::Matrix4d) EIGEN_MAKE_ALIGNED_OPERATOR_NEW };
This guarantees the class itself is always aligned to a 16-byte boundary, regardless of parent classes or other members.
3. Compiler Flags Are Overriding Alignment
Fedora 27 uses GCC 7.x, which has slightly different default alignment behavior compared to older versions. If you’ve added flags like -fno-align-functions or -fno-align-jumps, you might be disabling the alignment Eigen relies on for optimized operations.
Fix:
- Ensure your compile flags include
-msse2(or-march=native, which enables SSE2 on modern CPUs) — Eigen requires this for aligned memory operations. - Remove any flags that explicitly disable alignment optimizations.
4. Manual Memory Allocation Uses Non-Aligned Functions
If you’re using malloc/calloc to allocate NDTCell instances instead of new, standard heap allocation doesn’t guarantee 16-byte alignment (even though most systems do, edge cases exist that can trigger failures).
Fix:
Replace manual allocation with Eigen’s aligned memory functions:
// Allocate aligned memory NDTCell* cell = static_cast<NDTCell*>(Eigen::aligned_malloc(sizeof(NDTCell))); // Initialize the instance (if needed) new (cell) NDTCell(); // When freeing Eigen::aligned_free(cell);
Checking Valgrind Logs for Clues
Looking at your Valgrind output, pay close attention to the address where the invalid free occurs. If it’s not a multiple of 16, that confirms the instance wasn’t aligned when allocated. Trace back the allocation path — it’s almost always a container or manual allocation missing Eigen’s alignment handling.
内容的提问来源于stack exchange,提问作者Malcolm

