Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • Last Modified:

Memory leaks

Hello All,

How can I develop a tool that will find memory leaks in C code? Yes, I mean programmer allocated dynamically a bunch of memory and forgets to free them. This utility should find there are no references to this and free from heap.

Anybody has any idea??

Thanks
M
0
Mamata_gd
Asked:
Mamata_gd
  • 3
  • 2
  • 2
  • +3
2 Solutions
 
Jaime OlivaresSoftware ArchitectCommented:
Take a look to this project with source code:
http://freshmeat.net/projects/memwatch/?topic_id=47%2C809

0
 
Kent OlsenData Warehouse Architect / DBACommented:

Also look at http://valgrind.kde.org/

There are several ways to "write your own" that will help uncover a lot of the basic errors.


Kent
0
 
Mamata_gdAuthor Commented:
Is it for Windows system?

M
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Kent OlsenData Warehouse Architect / DBACommented:

valgrind doesn't work well with Windows GUI applications.  :(

Do you have any idea how many functions allocate or free memory?  Or how many times malloc() is called?

Kent
0
 
MysidiaCommented:
The main library calls you need to hook around to detect memory leaks in C are malloc and free
To detect other kinds of leaks (like files left open), you may also need to latch onto open() and close()

The thing needing to be checked to determine a leak is that the result of every malloc call is either freed or remains
reachable by a pointer (like it's data information in a data structure while the program is running).

Try looking for Boehm http://www.hpl.hp.com/personal/Hans_Boehm/gc/ for guidance
it's a garbage collector for C and C++

which is a step beyond what's needed to detect the common leak situation

0
 
guynumber5764Commented:
There are a number of excellent heap allocation and monitoring tools out there.  I used to use a commercial product under Windows called Smartheap which, at the time, had the additional advantage of being much faster than MS's malloc()/free().  Since there was a third party market, I expect that MS has "innovated" some cruddy heap browser into MSVC in the meantime.  I know it prompted them to fix their allocation...

Barring that, the simplest answer and one which might well give you what you need is to wrap malloc() and free() to include a printf or log message.  For example, you could create a function similar to:

malloc_ptr_t mymalloc(size_t size)
{
        malloc_ptr_t p;
        p = malloc(size);
        printf("malloc(%d)=%lx\n", size, p);
        return (malloc(size));
}

(caveat: pseudocode off the top of my head...may not compile or work).

and replace all references to "malloc" with "mymalloc" (use a #define but be careful not of affect the fn above).  Do the same for free and now you should be able to match your allocs and frees...
0
 
Mamata_gdAuthor Commented:
Yes, stubbing mallc and free is good idea. But, how do i know that pointer is not referenced for long time or there are no refrence to this allocation. In that case, the untility should go ahead and delete the allocation. how do I do that???

I am interested more in the deletion if not referenced? Meaning, how can I find out that there are no references to this allocation???

Thanks,
M
0
 
guynumber5764Commented:
The unfortumate and short answer is you don't because you can't.  What you are asking for is called "automatic garbage collection", a feature that is impossible to implement natively in C.  Its one of the big arguments for using Java.

In C, the only connection between a pointer variable and the heap is malloc()/free().  People have defined smart pointer classes that add  reference counts and/or LRU algorithms but they tend to have limited application and probably wouldn't solve your problem anyways.

The only real solution is to figure out where the memory is coming from and where it is supposed to be freed.  As a bonus, this will probably have the effect of simplifying your design and increasing correctness.  I would suggest starting with a code review that, at every malloc(),  asks the questions: "which object or module "owns" this memory (usually the allocator)" and "When and how does it get released?".
0
 
Kent OlsenData Warehouse Architect / DBACommented:

There are a couple of "tricks" that you can perform to see just how big the problem really is.  The first, and easiest, is just increment counters every time that malloc() or free() is called.  You probably need to conditionally increment when realloc() is called when passed a zero for the base address, too.

int MallocCount = 0;
int FreeCount = 0;
int ReAllocZeroCount = 0;

void *MyMalloc (size_t size)
{
  MallocCount++;
  return (malloc (size0);
}

void MyFree (void *Address)
{
  FreeCount++;
  free (Address);
}

void *MyReAlloc (void *Address, size_t *Size)
{
  if (Address == NULL)
    ReAllocZeroCount++;
  return (realloc (Address, Size));
}

Then right before the program exits, display the values of MallocCount, FreeCount, and ReAllocZeroCount.  This will tell you the number of buffers that you've assigned and how many that you've assigned that weren't free()ed.

With some enhancing, you can also learn the number of bytes of heap memory that are still assigned and even the address of the buffers so that you can dump/display them.


Kent
0
 
manojantonyCommented:
In Sun OS, bcheck utility is there ..
bcheck [-access | -all | -leaks | -memuse] [-o logfile ] [-q]  [-s script] program  [args]
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now