Memory management

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ycinar
    New Member
    • Oct 2007
    • 39

    Memory management

    Do we have to free the memory when we are stopping the program?

    I am currently dealing with a crash which is originated from Access violation reading location, i.e. Basically, program is trying to read a pointer which is not in the memory anymore. I can get around with it if i dont free that pointer on stopping the application which is not a best practice.

    Should I just set it to NULL? But I dont think it is the same thing as this is C++, not Java.

    Does anyone have any suggestions?
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    Originally posted by ycinar
    I can get around with it if i dont free that pointer on stopping the application which is not a best practice.
    Probably you have already freed that piece of memory. Now most of the implementation of malloc or modern day os are sophisticated enough that it would take back all memory allocated to your program. If i am wrong some body please do let me know. It would more easier to diagnose if you could put your code here

    Comment

    • ycinar
      New Member
      • Oct 2007
      • 39

      #3
      Originally posted by svlsr2000
      Probably you have already freed that piece of memory. Now most of the implementation of malloc or modern day os are sophisticated enough that it would take back all memory allocated to your program. If i am wrong some body please do let me know. It would more easier to diagnose if you could put your code here
      On exiting program;

      free (xyz);

      then at some stage, in another functions;

      xyz->a = b;

      because xyz was freed, (xyz->a = b;) will crash.

      That is my all problem. If I comment out free (xyz); no crash happens.

      Appreciate your help.

      Comment

      • ycinar
        New Member
        • Oct 2007
        • 39

        #4
        svlsr2000,

        When u say OS could take care of it, do you mean that i dont need to free the memory myself?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          That is correct. When your process dies, the operating system releases any resources that it had allocated.

          There's just no way that your leak is going to stop the entire computer.

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            Did you say C++? Then why use malloc and free? You should be using new and delete.

            You can't free a variable and then try to access it's memory in some way. Dereferencing a pointer would be such a kind of action. The result is a crash.

            A modern and non-small scale OS will most likely have a system to deal with memory leaks. The OS will clean up after your program as much as it can, including any unreleased memory. That does not mean you should be lax in your memory management. Use proper memory managed techniques like smart pointers and RAII and what not.

            Comment

            • ycinar
              New Member
              • Oct 2007
              • 39

              #7
              I am not using malloc. i am using new and free.

              It is an existing code i need to fix so.. Thanks for your all comments.. they all have been helpful.

              Comment

              Working...