Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Memory leak...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 6 Topic 45680 of 48612
Post > Topic >>

Re: Memory leakage problem

by Puppet_Sock <puppet_sock@[EMAIL PROTECTED] > May 1, 2008 at 07:11 AM

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
 




 6 Posts in Topic:
Memory leakage problem
luke <sirix@[EMAIL PRO  2008-05-01 12:17:50 
Re: Memory leakage problem
Lionel B <me@[EMAIL PR  2008-05-01 12:32:59 
Re: Memory leakage problem
Juha Nieminen <nospam@  2008-05-01 12:52:18 
Re: Memory leakage problem
luke <sirix@[EMAIL PRO  2008-05-01 15:22:06 
Re: Memory leakage problem
peter koch <peter.koch  2008-05-01 06:47:24 
Re: Memory leakage problem
Puppet_Sock <puppet_so  2008-05-01 07:11:21 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 15:28:46 CST 2008.