Registered Member
|
I have a templated class Container with a member m_F of type Eigen::PlainObjectBase. If I initialize the member by passing a parameter to the constructor of Container to the constructor of m_F in the constuctor initialization list, I get a memory violation. Here's a minimal example:
Compiling using clang++ 7.0 with Eigen version 3.2.4 on mac os x, running the result produces:
Replacing the constructor initialization list with a direct assignment by changing the lines
to
makes the error go away. Is there a reason the constructor initialization list initialization of m_F is causing a memory violation? |
Moderator
|
hm, actually you are not supposed to create PlainObjectBase<> objects. As its name stands, PlainObjectBase has been designed as an abstract base class. You should rather declare m_F as a DerivedF object type.
|
Registered Member
|
I'm worried about using Derived_F directly because in other situations this seems to result in a memory leak:
http://www.alecjacobson.com/weblog/?p=4413 I recently ran into a frustrating memory leak “gotcha” involving Eigen. The following code compiles
but causes a memory leak (without any warnings/asserts from Eigen):
The problem seems to be the cast happening in the return. Changing the return type of leaky to Derived:
Is this use of PlainObjectBase also discouraged? (It would be convenient if the code didn't compile rather than crash with memory violations, if possible). |
Moderator
|
Right, the core of the problem is still the same: this code creates a PlainObjectBase<Derived> object which is is not a Derived one. leaky() should rather return a Derived object. This will also make it faster thanks to RVO (https://en.wikipedia.org/wiki/Return_value_optimization).
I'll think about a way to disable the compilation of such code, but I cannot think about an easy solution right now. Maybe it will be simpler to fix these usages, even though they are not recommended... |
Moderator
|
Looks like these issues have already been solved in default branch (3.3-alpha1).
|
Moderator
|
I fixed it in 3.2 as well because many variants did worked (e.g, by passing PlainObjectBase ctor, multiplying the argument by 1, calling .block on the full matrix, etc.). But this possibility might be disabled in the future...
|
Registered users: Bing [Bot], Google [Bot], q.ignora, watchstar