vsamy / eigen-cddlib

Simple wrapper to use Eigen matrix with cddlib.
GNU General Public License v3.0
2 stars 8 forks source link

Memory corruption issue after calling "poly.setHrep(H, b)" #12

Open wyqsnddd opened 1 year ago

wyqsnddd commented 1 year ago

Hi Vincent!

I noticed an interesting issue recently. Following the test example, I wrapped eigen-cddlib's Polyhedron object in a function: vRepComdCone. It worked fine if I call the function once. As long as I try to call vRepComdCone consecutively, I got the following memory error:

#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff7681859 in __GI_abort () at abort.c:79
#2  0x00007ffff76ec26e in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff7816298 "%s\n") at ../sysdeps/posix/libc_fatal.c:155
#3  0x00007ffff76f42fc in malloc_printerr (str=str@entry=0x7ffff78189f8 "malloc(): memory corruption (fast)") at malloc.c:5347
#4  0x00007ffff76f743c in _int_malloc (av=av@entry=0x7ffff784bb80 <main_arena>, bytes=bytes@entry=32) at malloc.c:3594
#5  0x00007ffff76fab95 in __libc_calloc (n=<optimized out>, elem_size=<optimized out>) at malloc.c:3428
#6  0x00007ffff72f6ee6 in dd_InitializeArow () from /usr/lib/x86_64-linux-gnu/libcdd.so.0
#7  0x00007ffff72f6f5c in dd_InitializeAmatrix () from /usr/lib/x86_64-linux-gnu/libcdd.so.0
#8  0x00007ffff72f7512 in dd_CreateMatrix () from /usr/lib/x86_64-linux-gnu/libcdd.so.0
#9  0x00007ffff75de991 in Eigen::Polyhedron::initializeMatrixPtr (this=this@entry=0x7fffffffd2c0, rows=19, cols=4, isFromGenerators=isFromGenerators@entry=false)
    at /home/yuquan/local/eigen-cddlib/src/Polyhedron.cpp:112
#10 0x00007ffff75de9d0 in Eigen::Polyhedron::doubleDescription (this=this@entry=0x7fffffffd2c0, matrix=..., isFromGenerators=isFromGenerators@entry=false)
    at /usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:153
#11 0x00007ffff75ded99 in Eigen::Polyhedron::hvrep (this=0x7fffffffd2c0, A=..., b=..., isFromGenerators=false)
    at /home/yuquan/local/eigen-cddlib/src/Polyhedron.cpp:104
#12 0x00007ffff75dee23 in Eigen::Polyhedron::setHrep (this=this@entry=0x7fffffffd2c0, A=..., b=...) at /home/yuquan/local/eigen-cddlib/src/Polyhedron.cpp:50
#13 0x00007ffff7de52a2 in VelOpt::VelOptimizer::vRepComdCone (this=<optimized out>, nVel=@0x7fffffffd418: -0.14999999999999999)
    at /home/yuquan/local/impactvelocityoptimizer/src/VelOptimizer.cpp:53

For completeness, I also attached the function I made: vRepComdCone.txt

vsamy commented 1 year ago

It is difficult to tell without having minimal code that triggers the issue. Are you doing multithreading by any chance?

wyqsnddd commented 1 year ago

Hi Vincent!

I am not using multi-threading. Maybe I can try to comment out the mutex?

Or, I can try to construct Polyhedron for two times in the test program.

wyqsnddd commented 1 year ago

Hi Vincent,

I managed to call poly.setHrep(H, b) for two times after commenting out the dd_FreePolyhedra(polytope_); from the doubleDescription:

 bool Polyhedron::doubleDescription(const Eigen::MatrixXd& matrix, bool isFromGenerators)
{
    initializeMatrixPtr(matrix.rows(), matrix.cols(), isFromGenerators);

    for (auto row = 0; row < matrix.rows(); ++row)
        for (auto col = 0; col < matrix.cols(); ++col)
            matPtr_->matrix[row][col][0] = matrix(row, col);

    // if (polytope_ != nullptr)
    //     dd_FreePolyhedra(polytope_);

    polytope_ = dd_DDMatrix2Poly(matPtr_, &err_);
    return (err_ == dd_NoError) ? true : false;
}

Would you mind looking at a customized example?

wyqsnddd commented 1 year ago

Hi Vincent,

It seems that I found a way to fix the issue with the minimum changes: https://github.com/vsamy/eigen-cddlib/compare/master...wyqsnddd:eigen-cddlib:topic/memory-issue

Could you please take a look at it?

vsamy commented 1 year ago

You are removing all the free functions, so you are basically leaking memory right now

vsamy commented 1 year ago

Please provide a minimal code example so I can debug

wyqsnddd commented 1 year ago

Hi Samy, Thanks for the offer. I will try to come up with an example.