Connecting Tech Pros Worldwide Forums | Help | Site Map

Memory management

Member
 
Join Date: Oct 2007
Posts: 39
#1: Oct 31 '07
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?

Expert
 
Join Date: Feb 2007
Location: Bangalore
Posts: 182
#2: Oct 31 '07

re: Memory management


Quote:

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
Member
 
Join Date: Oct 2007
Posts: 39
#3: Oct 31 '07

re: Memory management


Quote:

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.
Member
 
Join Date: Oct 2007
Posts: 39
#4: Oct 31 '07

re: Memory management


svlsr2000,

When u say OS could take care of it, do you mean that i dont need to free the memory myself?
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#5: Oct 31 '07

re: Memory management


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.
Expert
 
Join Date: Aug 2007
Posts: 674
#6: Oct 31 '07

re: Memory management


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.
Member
 
Join Date: Oct 2007
Posts: 39
#7: Oct 31 '07

re: Memory management


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.
Reply