memory leak

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2008
Posts: 5
Reputation: sreesai is an unknown quantity at this point 
Solved Threads: 0
sreesai sreesai is offline Offline
Newbie Poster

memory leak

 
0
  #1
Aug 9th, 2008
Hi,
I want to know how to program to find memory leak. my idea is to find whether there is a free() for every malloc before exiting the function.

x()
{
malloc()
....

if()
{
abc
return; // thr is a memory leak as thr is no free n we are returning from the function.
}
.....
.....
free()
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: memory leak

 
0
  #2
Aug 9th, 2008
> my idea is to find whether there is a free() for every malloc before exiting the function.
Is this a memory leak?
  1. void foo ( char *msg ) {
  2. char *p = malloc( 10 );
  3. if ( strlen(msg) >= 10 ) {
  4. free( p );
  5. p = NULL;
  6. } else {
  7. strcpy( p, msg );
  8. }
  9. return p;
  10. }
  11.  
  12. int main ( ) {
  13. char *p = foo( "hello" );
  14. if ( p ) {
  15. printf( "Yay %s\n", p );
  16. free( p );
  17. }
  18. return 0;
  19. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: memory leak

 
1
  #3
Aug 9th, 2008
Must be char* foo ( const char *msg ) { , of course ...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: memory leak

 
0
  #4
Aug 9th, 2008
One way to do it is to write your own malloc() and free() functions which keep track of the allocated pointers in a linked list. Then just before program exit run through the linked list to find the pointers that have not been freed.
  1. struct node
  2. {
  3. unsigned int line_number;
  4. char* file;
  5. void* ptr;
  6. struct node* next;
  7. };
  8.  
  9. void* mymalloc(unsigned int size, unsigned ine line_number, const char*filename)
  10. {
  11. void* ptr = malloc(size);
  12. // now add the pointer to the linked list, not shown
  13.  
  14. return ptr;
  15. }
  16.  
  17. void myfree(void* ptr)
  18. {
  19. // search list for ptr and when (and if) found delete the node
  20. // code is not shown here
  21.  
  22. free(ptr);
  23. }
  24.  
  25. int main()
  26. {
  27. // example use. Note: I don't know if all compilers support __LINE__ and
  28. // __FILE__ macros or not. __LINE__ is a preprocessor directive that
  29. // returns the current line number of the file being compiled, and
  30. // __FILE__ is a preprocessor directive that returns the file name
  31. // of the current file being compiled.
  32. int* array = mymalloc( sizeof(int * 10), __LINE__, __FILE__);
  33.  
  34. }
Last edited by Ancient Dragon; Aug 9th, 2008 at 1:07 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 5
Reputation: sreesai is an unknown quantity at this point 
Solved Threads: 0
sreesai sreesai is offline Offline
Newbie Poster

Re: memory leak

 
0
  #5
Aug 9th, 2008
Thanks for the reply. But my requirement was different. This code works fine when you want to write new program. What i want is to find a missing free() in existing code. In a big application, you come across the below sub sections. Then how do you deal with it ? there needs to be a memory trailer (as a part of debugging)to check whether memory was freed before exiting.
  1. void foo ( char *msg ) {
  2. char *p = malloc( 10 );
  3. if ( strlen(msg) >= 10 ) -> at this point if free was missed ?
  4. return;
  5. else {
  6. strcpy( p, msg );
  7. }
  8. return p;
  9. }
  10. int main ( ) {
  11. char *p = foo( "hello" );
  12. if ( p ) {
  13. printf( "Yay %s\n", p );
  14. free( p );
  15. }
  16. return 0;
  17. }

Originally Posted by Salem View Post
> my idea is to find whether there is a free() for every malloc before exiting the function.
Is this a memory leak?
  1. void foo ( char *msg ) {
  2. char *p = malloc( 10 );
  3. if ( strlen(msg) >= 10 ) {
  4. free( p );
  5. p = NULL;
  6. } else {
  7. strcpy( p, msg );
  8. }
  9. return p;
  10. }
  11.  
  12. int main ( ) {
  13. char *p = foo( "hello" );
  14. if ( p ) {
  15. printf( "Yay %s\n", p );
  16. free( p );
  17. }
  18. return 0;
  19. }
Last edited by Ancient Dragon; Aug 9th, 2008 at 6:46 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: memory leak

 
0
  #6
Aug 9th, 2008
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: memory leak

 
0
  #7
Aug 10th, 2008
Which OS/Compiler are you using sreesai ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 5
Reputation: sreesai is an unknown quantity at this point 
Solved Threads: 0
sreesai sreesai is offline Offline
Newbie Poster

Re: memory leak

 
0
  #8
Aug 10th, 2008
Originally Posted by Salem View Post
Which OS/Compiler are you using sreesai ?
GCC .
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: memory leak

 
0
  #9
Aug 12th, 2008
Is this an assignment, just to go through and count occurrences of these particular functions within some *.c file?

I ask, because for most "REAL WORLD" programs you can't just merely write a script that counts the occurrences of malloc with the occurances of free and check that they're equal.

real world programs are not always nice and neat with a free following every malloc like how you expect HTML files always to always have a "close tag" occuring somewhere inline after its corresponding "open tag".

typically memory leaks occur because the program logic often calls functions or conditionally executes blocks of codes that contain either a malloc or a free -- but not both -- and the programmer misses some unexpected condition in the logic and therefore makes (or forgets) an unaccounted call to malloc or free.

memory allocation problems is the bane of program debugging. there are far too many programmers out there who think they're somehow "cool" because they use malloc, even though 99% of the time they don't need to.

combine this with the fact that many of these characters tend to write sloppy, convoluted code, and you'll begin to understand Why I F-ing Hate Malloc





.
Last edited by jephthah; Aug 12th, 2008 at 6:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: memory leak

 
0
  #10
Aug 13th, 2008
Well 'GCC' is only half an answer, so let me guess the other half of the answer and say the OS is 'Linux'.

In which case, try this command line
valgrind myprog
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC