Memory leaking - can't find where

Hi,

I am writing C++ code for Windows (win32 console) to determine when the active window changes (using VS C++ 2008). Using the Windows task manager, I can see that the application leaks a little memory the first time a new window is accessed. I have used several techniques to determine where the memory is leaking but I haven't been able to identify the source. Most tools don't report a leak at all but Task manager says otherwise. The amount of memory that leaks is not consistent (usually 8 or 12Kb per window change) and it does not correlate to the size of the window title.

Can anyone spot my error? Many thanks to anyone who can help - this is driving my batty :)


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
// vTitleTest4.cpp : Defines the entry point for the console application.
//

#include <windows.h>      //Needed for HWND
#include <iostream>       //Needed for cout
#define MAX 256

using namespace std;

// Global Variables
HWND g_hwndLastActiveWindow = NULL; // Handle to last active window

int main()
{
	int i;
	//	for ( i = 0 ; i < 10 ; i++ ) 
	for (;;) // Loop forever
	{
		//Show Window Title if it has changed since the last cycle
		HWND hwndCurrentActiveWindow = GetForegroundWindow();
		if(g_hwndLastActiveWindow != hwndCurrentActiveWindow)
		{
			char windowTitle[MAX + 10];
			strcpy(windowTitle,"[");
			GetWindowTextA(hwndCurrentActiveWindow,&windowTitle[1],MAX);
			strcat(windowTitle,"]");
			cout << windowTitle << endl;
			cout << "windowTitle Array Size = " << sizeof windowTitle << endl;
			cout << "windowTitle String size = " << strlen(windowTitle) << endl;
			cout << "hwndCurrentActiveWindow Size = " << sizeof hwndCurrentActiveWindow << endl;
			cout << "g_hwndLastActiveWindow Size = " << sizeof g_hwndLastActiveWindow << endl << endl;

			g_hwndLastActiveWindow = hwndCurrentActiveWindow;
		}
		Sleep(5000);
	}
	return 0;
}
Last edited on
Nope. I got nothing. What you saw must have been the stack growing.

If there is no dynamic memory allocation, there can be no memory leaks.
From the SDK docs on GetWindowText
To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.


I don't know if the OS is leaking somthing on your behalf. Probably not.
I've checked your application by Intel Parallel Inspector. There are not any problems.

http://software.intel.com/en-us/intel-parallel-inspector/
Last edited on
Wow, thank you to those of you who took the time to look into this! I suppose I misread the behavior I saw in Task Manager.

Denis - I have not used Parallel Inspector. I will definitely check it out.

Thanks again!
Peter
Most memory leaks are pretty obvious, they'll steadily consume unnecessarily large amounts of memory until the program is killed. (Most leaks I've created consumed hundreds of megs rather quickly.) Also, as was previously stated, memory leaks are impossible without dynamic allocation of objects, so your program is using memory as it should. Memory usage isn't a bad thing; if you need to use memory, you need to use memory.
Last edited on
I advise you to use some special tool for memory leaks detection and localization. For example - Deleaker.
http://deleaker.com/
You're replying to an almost month old post.

Actually, after seeing his past posts, is this guy a spambot or something?
Topic archived. No new replies allowed.