[CoinUtils] Possible memory leaks in CoinPackedMatrix
xavier warin
xavier.warin at gmail.com
Mon Aug 13 10:23:05 EDT 2007
Hi
I 'm a new user of coin Clp. I 'm moving from Lpsolve to coin which is far
more effective (a factor 4 for my application)
I use Osi-0.96.1
But in my application which uses a big number of linear problems to solve i
faced some problems of memory leaks with Coin.
I went back to the build.cpp Osi example that i slightly modified adding
delete.
// Example of using COIN-OR OSI, building the instance internally
// with sparse matrix object
#include <iostream>
#include "OsiClpSolverInterface.hpp"
#include "CoinPackedMatrix.hpp"
#include "CoinPackedVector.hpp"
int
main(void)
{
// Create a problem pointer. We use the base class here.
OsiSolverInterface *si;
// When we instantiate the object, we need a specific derived class.
si = new OsiClpSolverInterface;
// Build our own instance from scratch
/*
* This section adapted from Matt Galati's example
* on the COIN-OR Tutorial website.
*
* Problem from Bertsimas, Tsitsiklis page 21
*
* optimal solution: x* = (1,1)
*
* minimize -1 x0 - 1 x1
* s.t 1 x0 + 2 x1 <= 3
* 2 x0 + 1 x1 <= 3
* x0 >= 0
* x1 >= 0
*/
int n_cols = 2;
double *objective = new double[n_cols];//the objective coefficients
double *col_lb = new double[n_cols];//the column lower bounds
double *col_ub = new double[n_cols];//the column upper bounds
//Define the objective coefficients.
//minimize -1 x0 - 1 x1
objective[0] = -1.0;
objective[1] = -1.0;
//Define the variable lower/upper bounds.
// x0 >= 0 => 0 <= x0 <= infinity
// x1 >= 0 => 0 <= x1 <= infinity
col_lb[0] = 0.0;
col_lb[1] = 0.0;
col_ub[0] = si->getInfinity();
col_ub[1] = si->getInfinity();
int n_rows = 2;
double *row_lb = new double[n_rows]; //the row lower bounds
double *row_ub = new double[n_rows]; //the row upper bounds
//Define the constraint matrix.
CoinPackedMatrix *matrix = new CoinPackedMatrix(false,0,0);
matrix->setDimensions(0, n_cols);
//1 x0 + 2 x1 <= 3 => -infinity <= 1 x0 + 2 x2 <= 3
CoinPackedVector row1;
row1.insert(0, 1.0);
row1.insert(1, 2.0);
row_lb[0] = -1.0 * si->getInfinity();
row_ub[0] = 3.0;
matrix->appendRow(row1);
//2 x0 + 1 x1 <= 3 => -infinity <= 2 x0 + 1 x1 <= 3
CoinPackedVector row2;
row2.insert(0, 2.0);
row2.insert(1, 1.0);
row_lb[1] = -1.0 * si->getInfinity();
row_ub[1] = 3.0;
matrix->appendRow(row2);
//load the problem to OSI
si->loadProblem(*matrix, col_lb, col_ub, objective, row_lb, row_ub);
//write the MPS file to a file called example.mps
si->writeMps("example");
// Solve the (relaxation of the) problem
si->initialSolve();
// Check the solution
if ( si->isProvenOptimal() ) {
std::cout << "Found optimal solution!" << std::endl;
std::cout << "Objective value is " << si->getObjValue() << std::endl;
int n = si->getNumCols();
const double *solution;
solution = si->getColSolution();
// We could then print the solution or examine it.
} else {
std::cout << "Didn't find optimal solution." << std::endl;
// Could then check other status functions.
}
delete si, matrix;
delete [] row_lb;
delete [] row_ub;
delete [] col_lb;
delete [] col_ub ;
delete [] objective ;
return 0;
}
I used valgrind that gave me the following results :
==11412==
==11412== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 27 from 1)
==11412== malloc/free: in use at exit: 128 bytes in 5 blocks.
==11412== malloc/free: 1,427 allocs, 1,422 frees, 1,456,883 bytes allocated.
==11412== For counts of detected errors, rerun with: -v
==11412== searching for pointers to 5 not-freed blocks.
==11412== checked 146,200 bytes.
==11412==
==11412==
==11412== 8 bytes in 1 blocks are indirectly lost in loss record 1 of 5
==11412== at 0x401D7C1: operator new[](unsigned)
(vg_replace_malloc.c:195)
==11412== by 0x4238D2E:
CoinPackedMatrix::resizeForAddingMajorVectors(int, int const*) (in
/home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x423B5DB: CoinPackedMatrix::appendMajorVector(int, int
const*, double const*) (in
/home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x423B674:
CoinPackedMatrix::appendMajorVector(CoinPackedVectorBase const&) (in
/home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x423B6CB: CoinPackedMatrix::appendRow(CoinPackedVectorBase
const&) (in /home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x43C1EA7: (below main) (in /lib/tls/i686/cmov/libc-2.3.6.so
)
==11412==
==11412==
==11412== 12 bytes in 1 blocks are indirectly lost in loss record 2 of 5
==11412== at 0x401D7C1: operator new[](unsigned)
(vg_replace_malloc.c:195)
==11412== by 0x4238D1C:
CoinPackedMatrix::resizeForAddingMajorVectors(int, int const*) (in
/home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x423B5DB: CoinPackedMatrix::appendMajorVector(int, int
const*, double const*) (in
/home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x423B674:
CoinPackedMatrix::appendMajorVector(CoinPackedVectorBase const&) (in
/home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x423B6CB: CoinPackedMatrix::appendRow(CoinPackedVectorBase
const&) (in /home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x43C1EA7: (below main) (in /lib/tls/i686/cmov/libc-2.3.6.so
)
==11412==
==11412==
==11412== 16 bytes in 1 blocks are indirectly lost in loss record 3 of 5
==11412== at 0x401D7C1: operator new[](unsigned)
(vg_replace_malloc.c:195)
==11412== by 0x4238E07:
CoinPackedMatrix::resizeForAddingMajorVectors(int, int const*) (in
/home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x423B5DB: CoinPackedMatrix::appendMajorVector(int, int
const*, double const*) (in
/home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x423B674:
CoinPackedMatrix::appendMajorVector(CoinPackedVectorBase const&) (in
/home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x423B6CB: CoinPackedMatrix::appendRow(CoinPackedVectorBase
const&) (in /home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x43C1EA7: (below main) (in /lib/tls/i686/cmov/libc-2.3.6.so
)
==11412==
==11412==
==11412== 32 bytes in 1 blocks are indirectly lost in loss record 4 of 5
==11412== at 0x401D7C1: operator new[](unsigned)
(vg_replace_malloc.c:195)
==11412== by 0x4238E19:
CoinPackedMatrix::resizeForAddingMajorVectors(int, int const*) (in
/home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x423B5DB: CoinPackedMatrix::appendMajorVector(int, int
const*, double const*) (in
/home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x423B674:
CoinPackedMatrix::appendMajorVector(CoinPackedVectorBase const&) (in
/home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x423B6CB: CoinPackedMatrix::appendRow(CoinPackedVectorBase
const&) (in /home/warin/LOGICIEL_4.1/lib/libCoinUtils.so.0.0.0)
==11412== by 0x43C1EA7: (below main) (in /lib/tls/i686/cmov/libc-2.3.6.so
)
==11412==
==11412==
==11412== 128 (60 direct, 68 indirect) bytes in 1 blocks are definitely lost
in loss record 5 of 5
==11412== at 0x401DB31: operator new(unsigned) (vg_replace_malloc.c:163)
==11412== by 0x8048CCD: main (build.cpp:58)
==11412==
==11412== LEAK SUMMARY:
==11412== definitely lost: 60 bytes in 1 blocks.
==11412== indirectly lost: 68 bytes in 4 blocks.
==11412== possibly lost: 0 bytes in 0 blocks.
==11412== still reachable: 0 bytes in 0 blocks.
==11412== suppressed: 0 bytes in 0 blocks.
Is it a bug or a my mistake.
Sincerely yours
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://list.coin-or.org/pipermail/coinutils/attachments/20070813/2ca485e5/attachment.html
More information about the CoinUtils
mailing list