Its always a trouble for me to securely freeing allocated memory. By Securely I mean free all the memory. So, I have developed a simple method that might help me to do so. I didnt use it in any project, but I will, anyway,
here is the two file:
secmem.h
secmem.c
Test file:
test.c
Now how it work,
in the test.c, I have called secureAlloc, including the line number and file name.
at the end of the program, I have called freeAll() function.
When It runs the free all function it also print all the line number and the file name from where the allocation has been requested.
This helps a lot in case of debugging and make sure all the allocated memory has been freed.
Final Steps, whenever you are sure enough that all memory has been freed then add some macro in common header file and remove the freeAll() function
test.h
Hope, this will help
here is the two file:
secmem.h
Code:
#ifndef secmem_h #define secmem_h 1 #include<stdlib.h> void * secureAlloc(size_t size,int Line, char *FileName); short secureFree(void *ptr); short freeAll(); #endif
Code:
#include "secmem.h"
#include <stdio.h>
#include <string.h>
struct MemoryList
{
long MemoryAddress;
int line;
char fileName[20];
struct MemoryList *Next;
};
static struct MemoryList *Start,*Last;
void * secureAlloc(size_t size,int Line, char *FileName)
{
long newAddress,newAddress2;
newAddress=(long)malloc(size);
newAddress2=(long)malloc(sizeof(struct MemoryList));
if(Start==NULL)
{
Start=(struct MemoryList *)newAddress2;
Last=Start;
Last->MemoryAddress=newAddress;
Last->line=Line;
strcpy(Last->fileName,FileName);
}
else
{
Last->Next=(struct MemoryList *)newAddress2;
Last=(struct MemoryList *)newAddress2;
Last->MemoryAddress=newAddress;
Last->line=Line;
strcpy(Last->fileName,FileName);
}
return (void *)newAddress;
}
short secureFree(void *ptr)
{
struct MemoryList *Cont,*Prev;
long temp=(long)ptr;
if(ptr!=NULL)
{
free(ptr);
}
else
{
return 1;
}
Cont=Start;
Prev=Cont;
while(Cont)
{
if((long)Cont->MemoryAddress==temp)
{
if(Cont==Start)
{
Start=Cont->Next;
free(Cont);
if(Start==NULL)
{
Last=NULL;
}
return 0;
}
else
{
Prev->Next=Cont->Next;
free(Cont);
if(Prev->Next==NULL)
{
Last=Prev;
}
return 0;
}
}
else
{
Prev=Cont;
Cont=Cont->Next;
}
}
return 1;
}
short freeAll()
{
struct MemoryList *Cont,*Next;
int Count=0;
Cont=Start;
while(Cont)
{
Next=Cont->Next;
printf("File Name: %s, Line Number : %d\n",Cont->fileName,Cont->line);
free(Cont);
Cont=Next;
Count++;
}
printf("Total Freed : %d\n",Count);
}
test.c
Code:
#include <stdio.h>
#include "secmem.h"
struct test_
{
char *a;
char *b;
char *c;
struct test_ *Next;
}*test;
int main()
{
test=secureAlloc(sizeof(struct test_),__LINE__,__FILE__);
test->a=(char*)secureAlloc(10,__LINE__,__FILE__);
secureFree(test);
freeAll();
return 0;
}
Now how it work,
in the test.c, I have called secureAlloc, including the line number and file name.
at the end of the program, I have called freeAll() function.
When It runs the free all function it also print all the line number and the file name from where the allocation has been requested.
This helps a lot in case of debugging and make sure all the allocated memory has been freed.
Final Steps, whenever you are sure enough that all memory has been freed then add some macro in common header file and remove the freeAll() function
test.h
Code:
#include <stdio.h>
#include <stdlib.h>
//#include "secmem.h"
#define secureAlloc(a,b,c) malloc(a)
#define secureFree(a) free(a)
struct test_
{
char *a;
char *b;
char *c;
struct test_ *Next;
}*test;
int main()
{
test=secureAlloc(sizeof(struct test_),__LINE__,__FILE__);
test->a=(char*)secureAlloc(10,__LINE__,__FILE__);
secureFree(test);
// freeAll();
return 0;
}