Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.


Memory Leak Upon Program Exit?

  • Please log in to reply
No replies to this topic

#1 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer

  • Malware Response Team
  • 12,304 posts
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:12:10 PM

Posted 18 April 2008 - 06:38 PM

Hello, I have the following function:
std::string getTextOfCommand(char command[]){
			//adds "> tempfile.txt" to the command string, executes it with system()
			//reads the file to a string, deletes the file, and deletes the file.
	std::string retVar;
	char *holder;
	long unsigned int length;
	holder = new char[strlen(command)+16];
	strcat(holder," > tempfile.txt");
	delete [] holder;
	std::ifstream file("tempfile.txt",std::ios::in);
	length = file.tellg();
	holder = new char[length];
	delete [] holder;
	system("del /f tempfile.txt");
	return retVar;

I have it returning a string so that I can delete the character array "holder" before the program exits. However, I am curious: this program is designed to run for maybe 30 seconds max, and this function called maybe 3 or 4 times. If I allocate something with the new operator, and the program exits, does the Operating System keep the ram marked as used? Or is any ram used by a program released by the program upon exit?


EDIT: The wikipedia article here seems to say I dont have to worry about deleting the array if I plan to have it destroyed at exit anyway:

The memory leak would only last as long as the program was running. For example: if the lift's power were turned off the program would stop running. When power was turned on again, the program would restart and all the memory would be available again, and the slow process of leaking would start again.

Can anyone verify this?

Edited by Billy O'Neal, 18 April 2008 - 07:06 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

BC AdBot (Login to Remove)


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users