Not free'd malloc memory issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drhowarddrfine
    Recognized Expert Expert
    • Sep 2006
    • 7434

    Not free'd malloc memory issue

    Someone handed me a piece of code that works really well one time through but, if you call it a second time, it returns an offset from where it should point...I think.

    My code calls his function which returns a pointer to a node structure. This node, and all its children, are allocated with malloc and I have a 'free function' to free everything when I'm done.

    I've been calling his stuff just once and exiting with no problems but, today, I started calling it consecutively and it's as if the pointers to other nodes are correct at the beginning but making stuff up as it goes along.

    What I found is his code allocates memory for the file we're reading, creates the nodes and pointers to that data, then returns the top node pointer to me.

    As you can tell, the allocated nodes get free'd but the memory holding the file does not. This probably worked before because I exit the program at that point but, now, I have a new routine that calls it twice.

    The file pointer is set up as you would expect, fp=malloc(size of file), so on the second call, the same fp gets re-allocated at the same point in his function on the second call.

    The results give me three string, "cart", "order", "item". But the second call gives me "cart", "order", "cart". Which is why I think something's gotten offset somehow. The nodes themselves are still set up correctly, just the wrong content.

    So my question is, would you guys agree that not freeing that file pointer is the source of the issue here? I know anything's possible since I'm not showing the code but does someone have some insight as to why that is? I'm throwing that out since I am just not in the mood to work on his code and it would be a pain to edit down a simple sample for you. I'd hate to start on it and have someone later tell me that's not the problem.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    I assume this is C, not C++, since you're using malloc instead of new.

    Were you going to post any of the code?

    "... The file pointer is set up as you would expect, fp=malloc(size of file), ..."
    By file pointer, do you mean a FILE * variable?
    What do you mean here by "size of file"?

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Hard to tell without the code Doc but in relation to malloc and freeing memory then
      • Not freeing malloc'd memory is always an error and in the long term would lead to problems with memory resource running out.
      • If the program has not actually run out of memory then not freeing memory should not effect what happens the next time memory is allocated.
      • What you are describing, odd data values sounds more like not properly initialising pointers or writing over the endof allocated (or static) objects or using static objects where a malloced object should have been used. That is to say it is part of the logic of using the pointer rather than memory having been allocated and not freed.


      How long is the code anyway without editing?

      Comment

      • drhowarddrfine
        Recognized Expert Expert
        • Sep 2006
        • 7434

        #4
        @Banfa,
        You're thinking the same thing I was thinking but it helps to hear someone else say the same thing.

        I didn't supply the code because I'm just thinking out loud and it is pretty long.

        Comment

        Working...