Click here to Skip to main content
12,819,248 members (31,507 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 8 Apr 2008

Memory Leak detection Program without using any tools

, 8 Apr 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Without using any tools, This program will find the memory leak in your program


In market, there are a lot of tools for detecting the memory leaks. Here I made a program which has to include in your source file and it will tell the desired informations to find where memory leaks happen.

There are two files one is findLeak.c and other is findLeak.h . Source file is test.c , this is your program which you want to check memory leaks.

Concept is that when user call library function "malloc or calloc " for allocating dynamic memory, then we have to call our malloc or calloc (In case of my source code 'MyCalloc' function in place of 'calloc' and MyMalloc function in place of 'malloc'). So we have to define malloc to your malloc and in .c file you have to undef malloc and calloc By this way we can call our MyMalloc or MyCalloc function and in this function we will call another function which keeps all info about how much memory allocated. We will do the same for Library function "free" to deallocating memory( In my source file "MyFree" is used in plae of free function).

Using the code

Now for keeping information, i made a simple singly linked list. which will add info about memory when user call "malloc" or "calloc" function and will also keep information when user call "free" function.

By program you can easily understand.

Heder File : findLeak.h

#define   uint          unsigned int 
#define   cchar         const char  
#define  OutFile       "/home/asadulla/test/MemLeakInfo.txt"   // Just Suppose

#define  MAX_FILENAME_LENGTH    256
#define  calloc(objs, nSize)    MyCalloc (objs, nSize, __FILE__, __LINE__)
#define  malloc(nSize)          MyMalloc (nSize, __FILE__, __LINE__)
#define  free(rMem)             MyFree(rMem)

// This structure is keeping info about memory leak

struct InfoMem
    void *addr;
    uint nSize;
    char fileName[MAX_FILENAME_LENGTH];
    uint lineNumber;

typedef struct InfoMem infoMem;

//This is link list of InfoMem which keeps a List of memory Leak in a source file

struct LeakMem
    infoMem memData;
    struct LeakMem *nxt;

typedef struct LeakMem leakMem;

void WriteMemLeak(void);

void SubAddMemInfo(void *rMem, uint nSize,  cchar  *file, uint lno);

void SubAdd(infoMem alloc_info);

void ResetInfo(uint pos); //erase

void DeleteAll(void); //clear(void);

void *MyMalloc(uint size, cchar *file, uint line);

void *MyCalloc(uint elements, uint size, cchar * file, uint lno);

void  MyFree(void * mem_ref);

Source File : findLeak.c

#include    <span class="code-keyword"><stdio.h></span>

These two files (header file and source file) user has to include in his source file to which user want to find memory leak.

Source File for detecting memory leak is as Here you can gice your own source file and in this source file you have write onle line to include file #include"findLeak.h"

Testing : test.c

Code: c
#include<span class="code-keyword"><malloc.h></span>

Now user can compile these programmes in a console as :
> g++ test.c findLeak.c
and give a command for run
> ./a.out

then go to you your directory where you have defined a macro "OutFile" and open this defined file and you can see the results.

Output of above source file is as:

Code: output

Name of your Source File                 : test.c
Starting Address                         : 184960
Total size Of memory Leak                : 10 bytes
Line Number for which no DeAllocation    : 7

Name of your Source File                 : test.c
Starting Address                         : 187104
 Total size Of memory Leak               : 60 bytes
Line Number for which no DeAllocation    : 9

Name of your Source File                 : test.c
Starting Address                         : 184984
 Total size Of memory Leak               : 16 bytes
Line Number for which no DeAllocation    : 10
Now you can try to make you own Test Cases just like test.c.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

asadullah ansari
Software Developer (Senior) Huawei Technologies
India India
Me Not Small Not Big Also but I Can be Broken Easily. That's I...

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Member 963329426-Nov-12 17:24
memberMember 963329426-Nov-12 17:24 
GeneralReally good article Pin
jack2k8529-Sep-09 5:06
memberjack2k8529-Sep-09 5:06 
GeneralMy vote of 1 Pin
Frank Heimes14-Jan-09 0:25
memberFrank Heimes14-Jan-09 0:25 
GeneralVery good article. Though there is a flaw in it. Pin
Vipin Kr. Sharma14-Aug-08 2:27
memberVipin Kr. Sharma14-Aug-08 2:27 
GeneralGood start Pin
axelriet9-Apr-08 3:08
memberaxelriet9-Apr-08 3:08 
GeneralRe: Good start Pin
asadullah ansari9-Apr-08 3:51
memberasadullah ansari9-Apr-08 3:51 
good suggestion!!! I will do. This is my first article. I am new to this forum.
GeneralRe: Good start Pin
alramesh9-Apr-08 22:14
memberalramesh9-Apr-08 22:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 9 Apr 2008
Article Copyright 2008 by asadullah ansari
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid