Memory leakage problem

L

luke

Hi,
my program has a memory leak and I can't handle it.
Basically, there are two classes, class_a and class_b, and one
object of each class: class_a obj_a, and class_b obj_b.

One of the methods of class_a is basically

float* class_a::get_data(int n) {
float *to_return = new float[n];
.. //computing entries in to_return
return to_return;
}

and one of the methods of class_b is basically

void class_b::use_data() {
float* data;
data = pointer_to_class_a -> get_data(s) ;
.. //using data;
delete [] data;
}

where s and pointer_to_class_a are members of class_b, and the latter
points to obj_a.
The method use_data() is used many times in the program, and it's the
only place where get_data(int) is used. However, the size of the program
(reported by top in linux)
increases drastically in time (use_data is used many times a second).

What do I do wrong? (to be honest, there are some other uses of new in
the program, but I don't suppose they are significant).

Best,
Lukasz Grabowski
 
L

Lionel B

Hi,
my program has a memory leak and I can't handle it. Basically, there are
two classes, class_a and class_b, and one object of each class: class_a
obj_a, and class_b obj_b.

One of the methods of class_a is basically

float* class_a::get_data(int n) {
float *to_return = new float[n];
.. //computing entries in to_return
return to_return;
}

and one of the methods of class_b is basically

void class_b::use_data() {
float* data;
data = pointer_to_class_a -> get_data(s) ; .. //using data;
delete [] data;
}

where s and pointer_to_class_a are members of class_b, and the latter
points to obj_a.
The method use_data() is used many times in the program, and it's the
only place where get_data(int) is used. However, the size of the program
(reported by top in linux)
increases drastically in time (use_data is used many times a second).

What do I do wrong?

Are you sure you're not calling class_a::get_data() elsewhere in the
program and not subsequently deleting its returned pointer?

Otherwise...
(to be honest, there are some other uses of new in
the program, but I don't suppose they are significant).

....maybe they are indeed significant.

Perhaps a debugger could cast some light on this.

In any case, have you considered using std::vector rather than new/delete
in your get_data/use_data functions? Maybe something along the lines of:

class_a
{
std::vector<float> data;
};

std::vector<float>& class_a::get_data(int n) {
data.resize(n);
//compute entries in data
return data;
}

void class_b::use_data() {
std::vector<float>& data = pointer_to_class_a -> get_data(s);
// use data
}
 
P

peter koch

Hi,
my program has a memory leak and I can't handle it.
Basically, there are two classes, class_a and class_b, and one
object of each class: class_a obj_a, and class_b obj_b.

One of the methods of class_a is basically

float* class_a::get_data(int n) {
        float *to_return = new float[n];
        ..      //computing entries in to_return
        return to_return;

}

and one of the methods of class_b is basically

void class_b::use_data() {
        float* data;
        data = pointer_to_class_a -> get_data(s) ;
        .. //using data;
        delete [] data;

}

where s and pointer_to_class_a are members of class_b, and the latter
points to obj_a.
The method use_data() is used many times in the program, and it's the
only place where get_data(int) is used. However, the size of the program
(reported by top in linux)
increases drastically in time (use_data is used many times a second).

What do I do wrong? (to be honest, there are some other uses of new in
the program, but I don't suppose they are significant).
What you do wrong? So little code you show, but certainly using a
std::vector instead of all that manual book-keeping must be number one
on the list.

/Peter
 
P

Puppet_Sock

Hi,
my program has a memory leak and I can't handle it.
Basically, there are two classes, class_a and class_b, and one
object of each class: class_a obj_a, and class_b obj_b.

One of the methods of class_a is basically

float* class_a::get_data(int n) {
        float *to_return = new float[n];
        ..      //computing entries in to_return
        return to_return;

}

and one of the methods of class_b is basically

void class_b::use_data() {
        float* data;
        data = pointer_to_class_a -> get_data(s) ;
        .. //using data;
        delete [] data;

}

where s and pointer_to_class_a are members of class_b, and the latter
points to obj_a.
The method use_data() is used many times in the program, and it's the
only place where get_data(int) is used. However, the size of the program
(reported by top in linux)
increases drastically in time (use_data is used many times a second).

What do I do wrong? (to be honest, there are some other uses of new in
the program, but I don't suppose they are significant).

Program size is not necessarily a good indicator of a
memory leak. It may just be a thrashed free store.
The memory that is deleted may not be made available
for new memory allocations.

Of course, it could actually be a memory leak. That
may be the source of a thrashed free store. If you
are leaking a few bytes someplace, then grab a large
block, then leak a few more, then grab a large block,
you could be walking through memory leaving these
small blocks behind.

What did you do wrong? Well, "One of the methods of
class_a is basically" is basically one of the things
you did wrong. Post *actual* code, not stuff that is
almost the code.

Try to make a minimal compilable code that demonstrates
your problem. In doing this, you may find that dropping
a particular chunk of code removes the problem, and it
may be in some completely other part of the code.
Socks
 
L

luke

Dnia Thu, 01 May 2008 12:52:18 +0000, Juha Nieminen napisał(a):
Use valgrind. It will tell you if there's a memory leak (and where).

Wow. Thanks. I owe you few days of life, probably. The problem is where I
would never suspect it: in the libplotter library distributed with GNU
plotutils. For a record, here is a sample code that does nothing and
grows to, according to top, 5% of memory (on 512Mb laptop):

#include <plotter.h>

int main() {
XPlotter ploter(cin, cout, cerr);
ploter.openpl();
ploter.fspace (0, 0, 10, 10);
for(int i=0;i<100000;i++){
ploter.fontsize(20);
usleep(10);
}
ploter.closepl();
cin.get();
return 1;
}

Compile with -lX11 -lplotter -lXaw -lXmu -lXt -lSM -lICE -lXext -Wall -lm.
God damn it. I got pretty far with my program, but now it seems I will
need to change plotting library... :-(((


Best, Lukasz Grabowski
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top