[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
memory leak
From: |
John W. Eaton |
Subject: |
memory leak |
Date: |
Wed, 23 May 2007 22:23:38 -0400 |
On 23-May-2007, Michael Prinzinger wrote:
| Good Day!
|
| I have a problem with Octave potentially causing memory leaks.
| I use liboctave in a C++ project with matrices.
| The problem occurs, when I try to read a matrix, which I have saved
previously.
|
| Please have a look on the following code:
|
| FILE* eigenmatrix_file = fopen(filename, "w");
| if(eigenmatrix_file == NULL)
| {
| throw fileException("Error! in EigenMatrix::save(): can't open file!");
| }
Do you have a handler for this exception?
| MArray2<Complex>* m_array = new MArray2<Complex>(this->row, this->column,
*(new Complex(0.0, 0.0)));
*(new Complex(0.0, 0.0) is definitely a leak as there can be no code
to delete the memory you are allocating with new (you haven't saved
the pointer it returns). Also, I suspect there is no need for you to
be allocating m_array with new either.
| fread(m_array, sizeof((*m_array)(0)), sizeof(m_array), eigenmatrix_file);
This is probably the real source of your problem. Your m_array
variable is a pointer to a single object of class MArray2<Complex>,
not a pointer to an array of Complex values. Also, sizeof (m_array)
is probably not doing what you think it is.
I think you want something more like
MArray2<Complex> m_array = MArray2<Complex> (this->row, this->column);
size_t n = this->row * this->column;
fread (m_array.fortran_vec (), sizeof (Complex), n, eigenmatrix_file);
| This code runs fine with small matrices, and I have no problems
| whatsoever.
That's surprising.
jwe