C/memory leaks
Expert: Narendra - 11/5/2004
QuestionHi there, I am writing quite a large program in
C++ that runs continuously and unfortunatley has
some huge memory leaks. (Every iteration it
loses roughly 8-16kB) I've checked everything I
can think of and still have no idea where they
are coming from. Every new has a corresponding
delete. All vectors or large structures are
local and therefore give back their memory when
their methods return. I am using PostgreSQL and
every PQexec has a corresponding PQclear and I
only do PQconnectdb's once at the beginning. I
really can't think of anything else. Here's some
more background info: The program takes roughly
52MB of memory initially. My computer has
roughly 773MB. Every memory loss seems to be in
4kB segments. I am running on a SuSe Linux
machine.
The really weird thing about this is that I was
tracing the program in debug mode while watching
the free memory via the 'free' command. The
memory would drop at one point in the code,
however, the next iteration through it would
drop at another point of the code different than
the first point. It also seems to be dropping
everywhere in my code; I can't seem to pinpoint
the place. So any ideas? I've certainly run out
of them.
Thank-you so much for your time in advance!
Joe
AnswerWithout looking at the code, it is impossible to say why it is happening!
You may be changing the address of a pointer at some place and that can be the cause of the problem.
Try to use some memory profiler and find out the cause.
The best I have used is valgrind. I use it on Linux. I don't know if it comes on other platforms also.
Try it.
Also on Linux, there is a tool called "memprof" which gives a GUI and shows in which function the leak is happening.
If you are also using Linux and want me also to try, then give me your code. (Tar it and send it to my mail id: ssnkumar@yahoo.com)
-ssnkumar