On May 1, 8:17=A0am, luke <si...@[EMAIL PROTECTED]
> wrote:
> Hi,
> my program has a memory leak and I can't handle it.
> Basically, there are two cl*****, 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) {
> =A0 =A0 =A0 =A0 float *to_return =3D new float[n];
> =A0 =A0 =A0 =A0 .. =A0 =A0 =A0//computing entries in to_return
> =A0 =A0 =A0 =A0 return to_return;
>
> }
>
> and one of the methods of class_b is basically
>
> void class_b::use_data() {
> =A0 =A0 =A0 =A0 float* data;
> =A0 =A0 =A0 =A0 data =3D pointer_to_class_a -> get_data(s) ;
> =A0 =A0 =A0 =A0 .. //using data;
> =A0 =A0 =A0 =A0 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
> (re****ted 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


|