Free not freeing memory.

This is a discussion on Free not freeing memory. within the C Programming forums, part of the General Programming Boards category; Hi everyone, I've been working on this project for school for the last week now, and I've had this problem ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Free not freeing memory.

    Hi everyone,

    I've been working on this project for school for the last week now, and I've had this problem that I've been banging my head against for a couple of days now. Functionality of the program isnt an issue, I have that pretty down pat. I am having an issue with dynamic memory allocation. At the top of my program I allocate some memory to store the important data in, and then attempt to free it at the bottom when I am all finished with it. For whatever reason it wont free the memory that has been allocated.

    I have tried to figure out what is causing it to not free the memory, however I've had no luck. I've moved around the free() statements, and at some random points it will free it and not free it.
    .
    I have attached my project file containing main(). I would greatly appreciate any help that can be offered, or a pointer in the right direction.
    Almost forgot to include this. I am running Ubuntu 9.04 and GCC 4.3.3

    Thanks in advance,
    Tim
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You would probably get more help if you just post your relevant code(in code tag) here.

    For whatever reason it wont free the memory that has been allocated.
    First how do you know 'it won't free'? which part of program?
    And why your main is returning 1 ?!

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You are doing nothing in your code to confirm or deny free isn't working as designed. Are you expecting your variables that point to the allocated memory to be cleared by free?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by Dino View Post
    You are doing nothing in your code to confirm or deny free isn't working as designed. Are you expecting your variables that point to the allocated memory to be cleared by free?
    Running it through valgrind reports that all of the memory allocated has not been freed.

  5. #5
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    The 'free' function call in C returns void, so how do you know it isn't working? Are you expecting a call to 'free' to make your pointers point to NULL? Is that what makes you think it isn't working? Because if that is the case, a function call to free does not do this.

    Edit: didn't see the post you just made when i posted.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Can you try to comment out your processing loop(while loop) and printletterstats() and see if leak still exists?

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by Bayint Naung View Post
    Can you try to comment out your processing loop(while loop) and printletterstats() and see if leak still exists?
    Yes I tried to comment out the process and print loops and the memory leak still exists.

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    You're not including <string.h>. What about the WordUtils.h file?

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Ok this is odd. I have this code.
    Code:
    int main(int argc, char *argv[])
    {
    	LETTERSTATS *letterStatsArray = (LETTERSTATS*)malloc(sizeof(LETTERSTATS) * 5);
    	PANVOWELSTATS *panvowels = (PANVOWELSTATS*)malloc(sizeof(PANVOWELSTATS));
    	initializePanvowels(panvowels);
    	initializeLetterStats(letterStatsArray , 5);
    
    	freeLetterStats(letterStatsArray , 5);
    	free(letterStatsArray);
    	freePanvowelStats(panvowels);
    	free(panvowels);
    	return 0;
    }

    Code:
    $ gcc --version
    gcc (GCC) 4.4.4 20100630 (Red Hat 4.4.4-10)
    Copyright (C) 2010 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    $ valgrind ./a.out
    ==2094== Memcheck, a memory error detector
    ==2094== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
    ==2094== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
    ==2094== Command: ./a.out
    ==2094== 
    ==2094== 
    ==2094== HEAP SUMMARY:
    ==2094==     in use at exit: 0 bytes in 0 blocks
    ==2094==   total heap usage: 14 allocs, 14 frees, 1,284 bytes allocated
    ==2094== 
    ==2094== All heap blocks were freed -- no leaks are possible
    ==2094== 
    ==2094== For counts of detected and suppressed errors, rerun with: -v
    ==2094== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 12 from 8)
    Maybe did you forget to re-compile after making changes?
    Last edited by Bayint Naung; 10-08-2010 at 11:46 AM. Reason: more info.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Hmm that is indeed odd. What OS and Compiler are you using?

  11. #11
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    I would get rid of the dynamically allocated word variable in main and just use a standard character array. You're using it as a buffer anyway, so there's no good reason to dynamically allocate it.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I noticed the following issues in your program after a quick scan of the code:
    • You don't fclose() inFile. Since FILE* pointers usually involve some dynamic memory allocations, this is probably a memory leak.
    • If the file cannot be opened you return immediately without freeing letterStatsArray or panvowels.
    • Although you execute "(p+i) -> totalChars += 1;", I never see totalChars initialized anywhere. Remember that malloc() won't necessarily give you zeroed memory; memory allocated with malloc() can be initialized to anything whatsoever.
    • Same issue with shortest and longest. You malloc() memory for them, but you never set the memory to anything and then you go and execute
      Code:
      if(strlen(word) < strlen((p+i) ->shortest))
      This will access uninitialized memory and it will be undefined.
    • If argv[1] does not exist you will be asking fopen() to open the file NULL, which probably won't go over well.
    • It's a bad idea to hard-code 5 and 101 everywhere.
    • Because of the way you've created letters[], it's not a standard C string because it does not have a NULL string terminator. I didn't see any code that actually did something wrong, but you should be very careful with this because if you so much as called strlen(letters) you'd have a buffer overrun.


    Also, this is a bit useless, don't you think?
    Code:
    const int wordLength;
    [edit] Wow, there were about five posts while I was typing this up. Guess it wasn't much of a "quick scan" after all. I think most of my points are still relevant though. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Some third party libraries and even the C runtime can actually leak memory. Can you post the Valgrind output that you think is the proof of a memory leak?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    Valgrind should tell you to “Rerun with --leak-check=full to see details of leaked memory”... so why not try that?

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    memory leak != free() not working.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 08:55 PM
  2. How to read a txt file into an array
    By Hitsugaya_KK in forum C Programming
    Replies: 49
    Last Post: 08-22-2009, 03:22 PM
  3. help understanding free and memory leaks
    By mc61 in forum C Programming
    Replies: 4
    Last Post: 04-08-2008, 01:47 AM
  4. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  5. Allcoating memory and not freeing it
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 09-05-2001, 12:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21