?
Solved

Memory leak despite alloc's having matching frees

Posted on 2009-04-27
12
Medium Priority
?
337 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:collages
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24242457
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
 
LVL 1

Author Comment

by:collages
ID: 24242493
What would you recommend for a windows distro?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24242497
>> 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:collages
ID: 24242581
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
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 800 total points
ID: 24242584
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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 400 total points
ID: 24242628
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
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 800 total points
ID: 24243451
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
 
LVL 1

Author Comment

by:collages
ID: 24243884
You all readlly chipped in to help me solve this problem.  Thanks guys.
0
 
LVL 1

Author Comment

by:collages
ID: 24243903
Uh, I hit split points.. Not close question.  Please cancel this request.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24243910
you can still just close it yourself :)
0
 
LVL 1

Author Comment

by:collages
ID: 24243920
Yeah, whoops :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24243922
>> 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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question