Freeing unused memory crashes program

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

    Freeing unused memory crashes program

    After two days of working on this bug, I just got it working but I don't know why. I'm throwing this out there in hopes someone recognizes it but I think everyone will say it doesn't make sense.

    The code is for a simple xml parser. I find the xml file size, malloc that size and fread the xml. Close the file and scan the xml in memory, creating a linked list/tree structure, malloc'ing for more memory as more nodes are created. Each node is a struct that contains a pointer the next/previous node, element name, attributes, etc. There are typically less than 50 nodes in each file.

    Each node gets its own malloc'ed memory space and all that data is copied into each node/struct from the xml file. After parsing, I free the xml data in memory while keeping pointer to the root node. The root node is returned to the calling program which then searches for whatever node/elements it needs.

    What's thrown me for a loop is freeing the memory for the initial fread xml file causes the program to lose or change some of the pointers. And this only happens on my remote web server (Apache on CentOS), not locally in a test program that does the same thing. If I don't free the memory, it works everywhere but nothing points to that memory after it returns!

    I know someone's going to say, "need to see the code" but, as I said, I just now finished this and I'd have to think of a way to simplify things to show a test case and I need to go take a walk. For the moment I hope this clicks with someone for when I get back.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Sounds strange Doc. Working on one system and not on another like that speaks of undefined behaviour.

    Seeing the code would definately be useful but other things you can try are running you program using valgrind which performs various memory checks to make sure a program is using the memory it allocates correctly.

    I look forward to getting more details.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Set breakpoints on each call to free() and malloc().

      Write down each address that you malloc() and veify you free it only once.

      BTW: Are you setting freed pointers to 0? free() os OK with null pointer but you crash freeing already freed memiry.

      Are you using C++? If so consider using a handle a.k.a a smart pointer.

      Lastly, you are not by any chance passing addresses to your server? Can't do that or vice versa either.

      Comment

      • drhowarddrfine
        Recognized Expert Expert
        • Sep 2006
        • 7434

        #4
        Originally posted by weaknessforcats
        Set breakpoints on each call to free() and malloc().

        Write down each address that you malloc() and veify you free it only once.

        BTW: Are you setting freed pointers to 0? free() os OK with null pointer but you crash freeing already freed memiry.

        Are you using C++? If so consider using a handle a.k.a a smart pointer.

        Lastly, you are not by any chance passing addresses to your server? Can't do that or vice versa either.
        Set breakpoints on each call to free() and malloc().

        Write down each address that you malloc() and veify you free it only once.
        Done that but feel a need to try again later.
        BTW: Are you setting freed pointers to 0? free() os OK with null pointer but you crash freeing already freed memiry.
        These pointers are used only once.
        Are you using C++?
        Oh, please.
        Lastly, you are not by any chance passing addresses to your server? Can't do that or vice versa either.
        Oh, please.

        Went out of town which is why I didn't get back right away. I'll have to get back into this. Thanks.

        Comment

        Working...