Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with memory leak from someone else's driver

I am running a c program on a CentOS 5.5 computer. The program runs a loop that performs tests over and over until told to stop by an external source.

By necessity I am using an old driver for a PCI card that communicates with my test system. Since upgrading from CentOS 4.5 to 5.5 I have noticed I can only loop through my program 175 times. At that time the program halts with an error allocating heap memory. I can watch the memory being used in chunks of 10 to 20 MB each time the program loops and the system just runs out of memory. When I exit the program using Cntrl-C the memory is immediately freed.

I have used Valgrind which indicates the memory leaks are in the old driver. The company that wrote the driver only supports Windows now and they haven't upgraded the driver in over 5 years.

Without the source code is there any way I can free the memory used by the driver each time I loop through my program?

Thanks.

like image 834
Gmandude Avatar asked Nov 15 '10 18:11

Gmandude


1 Answers

If you declare the access to the test system through the driver inside the loop, this should put it out of scope on each iteration.

Something like the following:

char readbuf[512];
for (int i = 0; i < countloops; i++) 
{
   int fd = open("/dev/com1", O_RDWR);
   readbuf = read(fd, sizeof (readbuf));
   close (fd);
}
like image 176
KevinDTimm Avatar answered Oct 02 '22 15:10

KevinDTimm