• C

Memory leak despite alloc's having matching frees

All of my alloc's contain matching free's and everything is executing fine, however when I run it in an infinite loop the memory usage continues to climb pretty quickly.  How can I go about hunting down this memory leak?
LVL 1
collagesAsked:
Who is Participating?
 
mrjoltcolaCommented:
Electric Fence has a port for Win32. I've never tried it on Windows, but you can find it on Google.

You can also download Purify trial for free and use it this once.

http://www-01.ibm.com/software/awdtools/purify/

Or, if your code is not windows specific, you can install Cygwin or MinGW and compile on Windows under the Linux-ish environment.

Valgrind is UNIX only at this time, to my knowledge.

0
 
mrjoltcolaCommented:
There are 2 options I know of, the first practical and the latter impractical in most cases:

Practical:
1) You can use a debugging memory manager like valgrind or electricfence to help. You would not run in production like this, this is a "development only" tool.

http://valgrind.org/

Both come standard in most Linux distributions, and are mostly a matter of relinking your program with the right library (-lefence for example)


2) A garbage collector, but you need to give over all of your memory management to it. It is not overly portable, but it does work in practice. One example is the boehm collector, which can be used in C and C++ applications, but there are others. This is an advanced option, and probably not the solution you are asking for since its a major step.

0
 
collagesAuthor Commented:
What would you recommend for a windows distro?
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
Infinity08Commented:
>> All of my alloc's contain matching free's

Are all of the matching free's done inside the infinite loop ? Could you show the code ? Or is it too big ?
0
 
collagesAuthor Commented:
It's a small subset of the main project I use to test so it's not too bad to post.  The large function which I removed and added a comment has the allocations that allocate the struct.  (all the allocations are the sames ones that have the frees in the free function)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct
{
	char **strParams;
	int intParamCount;
} NetPacket;
 
typedef struct
{
	NetPacket **packets;
	int intPacketCount;
} NetPacketCollection;
 
NetPacketCollection* ParsePackets(char *strBuffer)
{
   //...Allocations done here for the struct...
}
 
void freeNetPacketCollection( NetPacketCollection *netPacketCollection)
{
	int packetIndex, paramIndex;
	for(packetIndex = 0; packetIndex < netPacketCollection->intPacketCount; packetIndex++)
	{
		for(paramIndex =0; paramIndex < netPacketCollection->packets[packetIndex]->intParamCount; paramIndex++)
		{
 
			free(netPacketCollection->packets[packetIndex]->strParams[paramIndex]);
			netPacketCollection->packets[packetIndex]->strParams[paramIndex]=NULL;
 
		}
	
		free(netPacketCollection->packets[packetIndex]);
		netPacketCollection->packets[packetIndex] = NULL;
 
	}
	
	free(netPacketCollection->packets);
	netPacketCollection->packets=NULL;
	
	free(netPacketCollection);
	netPacketCollection = NULL;
}
 
main()
{
	while(1)
	{
		int packetIndex, parameterIndex;
		
		NetPacketCollection *netPacketCollection;
		netPacketCollection = ParsePackets("DATAHERE");
		for(packetIndex = 0; packetIndex < netPacketCollection->intPacketCount; packetIndex++)
		{
			printf("--Packet--\n");
			for(parameterIndex = 0; parameterIndex < netPacketCollection->packets[packetIndex]->intParamCount; parameterIndex++)
				{
					printf("Param %d  = %s\n", parameterIndex+1, netPacketCollection->packets[packetIndex]->strParams[parameterIndex]);
				}
			printf("\n");
		}
		freeNetPacketCollection(netPacketCollection);
		printf("-- End --\n");
	}
	return 0;
}

Open in new window

0
 
Infinity08Commented:
You never free netPacketCollection->packets[packetIndex]->strParams.

Also make sure that the intParamCount and intPacketCount are accurate - ie. that they always contain the correct amount of params resp. packets.
0
 
evilrixSenior Software Engineer (Avast)Commented:
If you are developing this on Linux then Valgrind is your best bet, as mrjoltcola has already identified, if you you can use the Debug CRT to track and identify memory leaks.

You can use _CrtSetDbgFlag to enable CRT heap allocation debugging. This should be at the very start of your program.
E.g. _CrtSetDbgFlag ( _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ) | _CRTDBG_LEAK_CHECK_DF );
http://msdn2.microsoft.com/en-us/library/974tc9t1(VS.80).aspx

You can use _CrtDumpMemoryLeaks to generate an error report if the application failed to free all the memory it allocated. This should be at the very end of your program.
http://msdn2.microsoft.com/en-us/library/d41t22sb(VS.80).aspx

Use can use _CrtSetBreakAlloc or _crtBreakAlloc to set break points where specific heap is allocated (as reported by CrtDumpMemoryLeaks) so that you can see where the problem starts
http://support.microsoft.com/kb/151585

Memory leak detection and isolation: http://msdn2.microsoft.com/en-us/library/x98tx3cf(VS.80).aspx
0
 
collagesAuthor Commented:
You all readlly chipped in to help me solve this problem.  Thanks guys.
0
 
collagesAuthor Commented:
Uh, I hit split points.. Not close question.  Please cancel this request.
0
 
evilrixSenior Software Engineer (Avast)Commented:
you can still just close it yourself :)
0
 
collagesAuthor Commented:
Yeah, whoops :)
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> You all readlly chipped in to help me solve this problem.  Thanks guys.

You are most welcome. Tracking down memory leaks can be a right royal pain in the rear end :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.