Memory alignment errors using Eigen with a dynamic library with Visual Studio 2013

I use Eigen in my software, and today I ran into a problem caused by changing my code to create a static library for a dynamic library on Windows using Visual Studio 2013. The reason for this switch was not related to Eigen.

I embed Eigen in my own library file, which itself is then bound to my applications. As mentioned earlier, this library was before the static library until today; I just updated my codebase to generate a dll file.

Since making this change, I get the following error message from Visual Studio:

The block in -------------------- has been allocated using aligned routines, use _aligned_free ()

(this message appears many times with different addresses every time, I used the dash above, since I do not think that specific addresses are related to this problem).

selecting "repeat" opens the debugger on line 255 on Memory.h

Visual studio IntelliSense (if there is no debugging) assumes that EIGEN_ALIGN and EIGEN_HAS_MM_MALLOC are both defined as 1, EIGEN_MALLOC_ALREADY_ALIGNED and EIGEN_HAS_POSIX_MEMALIGN are both undefined. Accordingly, it should be run _mm_free (ptr), which (again based on IntelliSense) is an alias for _aligned_free (a). So it looks like this code should work with the correct function, but it is not.

When I change the code back to the static library, this question disappears.

The only thing that is remotely relevant that I found from numerous Google searches is an article from Intel Fortran Compiler, which says that this error message can be obtained from a library that was compiled in an earlier version, which is called by code compiled with the latest version. In addition to using Visual Studio C ++ 2013, I rebuilt the code several times to make sure it was completely recompiled from a clean state, and this error message is saved.

I downloaded the latest code from mercurial repo (default branch), but this did not solve the problem.

I tried to be as thorough as I could. If you need more information, please let me know.

Edit:

Further context:

The "client code" that the DLL uses in this case is Google Test; The error message appears after testing the wait - that is, the class in the DLL file starts the destructor to clear the temporary object. I'm not trying to do evil things, for example, allocate memory in a DLL file, and then de-allocate it in the driver application - partly because I find this so confusing ....

+6
source share
1 answer

I had this exact problem. I converted my static library that uses Eigen for the DLL and started getting these errors during unit testing with gtest. Since there is no solution, I will tell you what I did to solve the problem. Essentially, the problem is that you created a new interface for the class containing Eigen matrices / vectors as member variables, and this interface creates a pointer to the class with your Eigen member variables.

First, if you have member variables that are Eigen matrices or vectors, then you should read this . In the end you need to add

public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW

to class definitions in which you use Eigen as member variables. Presumably, you will also receive warnings about warning C4316: object allocated on the heap may not be aligned 16 if you are using the Visual Studio compiler.

Now I still had problems after I used EIGEN_MAKE_ALIGNED_OPERATOR_NEW , this was due to the use of the atomic class Eigen matrix; boost::atomic<Eigen::MatrixXf> as a member variable. I believe alignment is very important if you guarantee atomicity.

+2
source

Source: https://habr.com/ru/post/976317/


All Articles