How to find memory leaks

Hello everyone. I am just starting out with C++ and I am finding some aspects of it quite confusing. I have been lurking on this forum for a few weeks and it has been immeasurably helpful!

I have been using process.h to thread my application, but I have not always used _endthread; if I didn't use it in this code, would it cause a memory leak?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <conio.h>
#include <iostream>
#include <windows.h>
#include <process.h>
using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
//GetStdHandle(nStdHandle) - Retrieves a handle to the specified standard device (standard input, standard output, or standard error).
	//STD_INPUT_HANDLE  - The standard input device. Initially, this is the console input buffer, CONIN$.
	//STD_OUTPUT_HANDLE - The standard output device. Initially, this is the active console screen buffer, CONOUT$.
	//STD_ERROR_HANDLE  - The standard error device. Initially, this is the active console screen buffer, CONOUT$.

COORD CursorPosition;
void PlaceCursor(int x, int y); 
void CheckKey( void *dummy );
bool loop = true;
int x = 20;
int y = 20;
int main(){
	cout << "WELCOME!" << endl;
	cout << "USE 'W', 'S', 'A' AND 'D' TO MOVE" << endl;
	cout << "PRESS 'P' TO EXIT" << endl;
	PlaceCursor(x, y);
	_beginthread( CheckKey, 0, NULL  );
	while(loop);
	return 0;
}

void PlaceCursor(int x, int y) 
{ 
CursorPosition.X = x; // Locates column
CursorPosition.Y = y; // Locates Row
SetConsoleCursorPosition(console,CursorPosition); // Sets position for next thing to be printed 
}
void CheckKey( void *dummy )
{
	while (loop){
		int key = _getch();
		if( (key == 119) || (key == 87) )//w || W
			y--;
		else if( (key == 115) || (key == 83) )//s || S
			y++;
		if( (key == 97) || (key == 65) )//a || A
			x--;
		else if( (key == 100) || (key == 68) )//d || D
			x++;
		else if( (key == 112) || (key == 80) ){//p || P
			loop = false;
			_endthread();
		}
		PlaceCursor(x, y);
		//Sleep(500);
	}
}

Also, when using winsock.h, if the sockets are not closed or WSACleanup is not called, is that a memory leak?

Thank you in advance! :)
Last edited on
No.

It is better to control the thread loop with an event.
Last edited on
How do I do that? Are you saying that if i didn't have _endthread there it wouldn't cause a memory leak? When do I need to use _endthread to free up memory?
Last edited on
Also, what is the best way to detect a memory leak? I use windows and VC++ 2010
Last edited on
Are you saying that if i didn't have _endthread there it wouldn't cause a memory leak?
Yes.

When do I need to use _endthread to free up memory?
You don't need to.

You can call _endthread or _endthreadex explicitly to terminate a thread; however, _endthread or _endthreadex is called automatically when the thread returns from the routine passed as a parameter.
http://msdn.microsoft.com/en-us/library/kdzttdcb%28v=vs.71%29.aspx
I'm sorry, what is a routine passed as a parameter? Thank you for the help.
The thread function, CheckKey() in your case.
So does that mean when it reaches the end of the thread it will terminate the thread and free memory?

What happens to a thread if you close the window before the thread finishes?

Or, what if I didn't make 'loop' false and just closed the console? Would the thread ever terminate?
Last edited on
So does that mean when it reaches the end of the thread it will terminate the thread and free memory?
I'm interested to hear what you think the thread memory is. There is thread local storage, but that doesn't apply here as you're not using it. The C runtime creates per thread data, but that's cleaned up by endthread().

What happens to a thread if you close the window before the thread finishes?
If you close the app, the operating system cleans resources acquired by the process.

Or, what if I didn't make 'loop' false and just closed the console? Would the thread ever terminate?
The main thread would terminate, causing the process to end.
Last edited on
Thanks for your continued patience!

I'm interested to hear what you think the thread memory is.

I am very new to C++, to be honest I don't know. Could you please recommend any reading for it?

If you close the app, the operating system cleans resources acquired by the process.

When does it not do that? (Because that's a memory leak, right?)

Thank you for your help.
Topic archived. No new replies allowed.