Jump to content

Memory Leak

- - - - -

  • Please log in to reply
8 replies to this topic

#1
espdev-darkness

espdev-darkness

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Hey codecall, so I just learned a bit about the heap and allocating memory from my C++ book. In the book they showed how memory leaks occur (not using delete or re-pointing before deleting). Using my devilish brain I decided to make a program that creates memory leaks until it is exited. Its very simple only 35 lines of code and allows the user to enter how fast they would like to crash their computer. Here it is:

//Memory Leak


#include <iostream>


using namespace std;


//Function Prototype

void createLeak(int &allocation);


int main()

{

	//Variable Declaration

	int allocationAmmount = 100;

	int incrementSpeed = 1;


	cout << "Welcome to the Memory Leak Program, enjoy the ride and watchout for the crash!\n" << endl;

	do

	{

		cout << "Please enter the speed in which you would like to crash your computer (1-100): ";

		cin >> incrementSpeed;

	}	while (incrementSpeed < 1 || incrementSpeed > 100);


	while(true)

	{

		allocationAmmount += incrementSpeed;

		createLeak(allocationAmmount);

	}


	return 0;

}


void createLeak(int &allocation)

{

	int* leak = new int(allocation);

}


I know this program works for I used it and then looked at task manager. When incrementSpeed = 2 it allocates about 1GB of memory in 5 seconds. Im assuming this would crash a computer once it used up all the RAM and moved to the pagefile. Although I am not sure and I do not want to test, nor do I want you guys to test. Basicly Im wondering if windows has anyway of detecting this from what I can tell it doesn't. Thanks!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You won't crash the OS, just the program when it can't allocate any more memory.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
espdev-darkness

espdev-darkness

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
...I was hoping this one would pull through. Whatever, anyways I won't ask you to show me how I could crash the OS because that would be like giving children guns. Thanks for the inference.

edit: I ran it in debug mode here was the output:
Unhandled exception at 0x7600b727 in training.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003af64c.

When I just run the executable it crashes at 3.72GB/4GB.

#4
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
If you really want to crash a computer, make a fork bomb.
It doesn't really "crash" a computer though, it just puts it in an unusable state, forcing the user to reboot.
Latinamne loqueris?

#5
espdev-darkness

espdev-darkness

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Better yet I made a troll bomb!


// Troll Bomb

#include <iostream>

#include <string>

#include <Windows.h>

using namespace std;


int main()

{

	int trollCount = 0;

	int trollEnd;

	cout << "Welcome to the Troll Bomb, the fun trolling program for kids!\n" << endl;

	do

	{

		cout << "How many times do you want to get trolled? (1 - 25): ";

		cin >> trollEnd;

	}	while (trollEnd < 1 || trollEnd > 25);


	while(trollCount < trollEnd)

	{

		ShellExecuteW(0, (LPCWSTR) TEXT("open"), (LPCWSTR) TEXT("http://dagobah.net/flash/successful_troll.swf"), NULL, NULL, 1);

		++trollCount;

	}

}


I put a limit of 25 on it because well you try having that music play slightly off unison 25 times over.

Disclaimer: I take no responsibility for epilectic seizures, try at your own risk.

#6
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
When I tried compiling/running that (curiosity overtook me), it just returned after the user input.
Latinamne loqueris?

#7
espdev-darkness

espdev-darkness

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
http://ompldr.org/vNmNlZQ
Start just before I hit enter.
http://ompldr.org/vNmNlZw
After I hit enter 5 new tabs popup in browser playing a troll flash file.

If the browser isn't open already it will only open one tab no matter how many times you enter, probably some antispam mechanism of IE8.

#8
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
This fork bomb will multiply nice and slow:
#include <cstdio>

#include <windows.h>

#define SLEEP_TIME 5000		/*This number controls the multiplication speed of the

							fork bomb. The larger the number, the slower it is*/


int main(int argc,char **argv)

{

	STARTUPINFO si;

	PROCESS_INFORMATION pi;

	ZeroMemory(&si,sizeof(si));

	si.cb=sizeof(si);

	printf("HELLO\n");		//Each child shares the same window, so each one

							//will display this message when they start.

	while(1)

	{

		Sleep(SLEEP_TIME);

		CreateProcess(argv[0],NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);

	}

}

You can use a program like Process Explorer to watch it multiply. You can also use the program to kill all the children.

And as a general disclaimer, RUN THIS AT YOUR OWN RISK. I am not responsible if you lose some data because you ran this program.
Latinamne loqueris?

#9
espdev-darkness

espdev-darkness

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
I still like my troll bomb...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users