You are here:

C/memory leaks

Advertisement


Question
Hi 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  

Answer
Without 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

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.