This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen

Bug 1049

Summary: std::make_shared fails to fulfill structure aliment
Product: Eigen Reporter: Valentin Roussellet <val.roussellet>
Component: Core - generalAssignee: Nobody <eigen.nobody>
Status: RESOLVED WORKSFORME    
Severity: Feature Request CC: chtz, gael.guennebaud, jacob.benoit.1, xstamatopoulos
Priority: Lowest    
Version: 3.2   
Hardware: x86 - 32-bit   
OS: All   
Whiteboard:
Bug Depends on:    
Bug Blocks: 814    

Description Valentin Roussellet 2015-07-29 10:15:02 UTC
As described in the doc, structures having Eigen aligned members (e.g. Vector4) may be misaligned when created on the heap with new.

http://eigen.tuxfamily.org/dox/group__TopicStructHavingEigenMembers.html

The solution for that issue is to overload the operator new, which is provided as a convenience macro to add in the said struct.

However std::make_shared ignores the overladed operator new as, in at least two implementations of the STD (GNU and MSCV) actually calls the global ::new.

These two codes, while semantically equivalent, behave differently.

std::shared_ptr<Foo> = std:::make_shared<Foo>(...)
std::shared_ptr<Foo> = std:::shared_ptr<Foo>(new Foo(...))

The second one yielding correct alignment while the first one may return a non-aligned pointer.

Would another macro specializing std::make_shared for all types requiring alignment be useful or practical ? 

If not, should Eigen provide a make_aligned_shared<T> implementation ?
Comment 1 Gael Guennebaud 2015-08-08 10:00:16 UTC
In C++11 there is std::allocate_shared that you can use with Eigen's aligned allocator.
Comment 2 Christos 2017-06-14 05:35:27 UTC
(In reply to Gael Guennebaud from comment #1)
> In C++11 there is std::allocate_shared that you can use with Eigen's aligned
> allocator.

std::allocate_shared fails as well in VS2017.
Not sure if it specific to msvc or not.

The code below is enough to reproduce it

class Foo
{
   Vec4d v;
public:
   EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};

auto test = std::allocate_shared<Foo>(Eigen::aligned_allocator<Foo>());
Comment 3 Gael Guennebaud 2019-02-18 20:31:52 UTC
Sorry for get back so late.

Works for me with gcc/clang (while make_shared does fail as expected).

If it don't work with MSVC, then this needs to be reported to VS team.
Comment 4 Nobody 2019-12-04 14:49:07 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to gitlab.com's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.com/libeigen/eigen/issues/1049.