473,382 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Memory leakage problem

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
Jun 27 '08 #1
5 1175
On Thu, 01 May 2008 12:17:50 +0000, luke wrote:
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<floatdata;
};

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
}

--
Lionel B
Jun 27 '08 #2
luke wrote:
However, the size of the program
(reported by top in linux)
Use valgrind. It will tell you if there's a memory leak (and where).
Jun 27 '08 #3
On 1 Maj, 14:17, luke <si...@poczta.onet.plwrote:
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
Jun 27 '08 #4
On May 1, 8:17*am, luke <si...@poczta.onet.plwrote:
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
Jun 27 '08 #5
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
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: frustrated | last post by:
Before I begin, I must first make the following disclaimer: Although I have considerable programming experience, I do not consider myself by any means to be an expert C++ programmer. The following...
2
by: Sambucus | last post by:
Hi group! I am using C++ and java with JNI to get some text in a RICHEDIT to my java program. I do so by accessing a C++ method every second. It all works fine except that it leaks memory every...
7
by: andylcx | last post by:
hi all: I have a code like below, is there any serious memory leakage in my code. I am confusion now but no idea how to fix it. In the member function of class A, I create a new object of class B...
10
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions:...
18
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
1
by: Gaël | last post by:
Hi everybody! I have a really big problem with ASP.NET application. I noticed that the w3wp.exe memory size, increase with the time and the use of my website. When it raise a certain value, w3wp...
2
by: chets | last post by:
Hi all Can anyone tell me what is the difference between:- *p=q; and p=&q; in C where declaration is like this char **p; char *q; Because if I do *p=q; in one file after mallocing q and...
0
by: kiran kumar | last post by:
Hi All, I am working on embedded python on C these days. I feel there is a memory leakage in this code. I have used our own memory pool and all the python code will use the heap from this memory...
14
by: madhawi | last post by:
i want to know that on what situation memory leakage happan and what is the solution to solve the problem of memory leakage.
11
by: prpradip | last post by:
I have an ImageList (_imageList). In _imageList I have put large numbers of Icons. Now what I need is to get Handle of all Icons that I put in _imageList, so that I can destroy (DestoryIcon) them...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.