Strange win32 memory problem

I am having a very interesting problem with one of my very simple win32 applications. The whole thing is basically the auto-generated project that Visual C++ gives you, except I have added a dialog box.

The dialog box is my problem. I am taking user input from an edit box, using GlobalAlloc() to free up memory for the string and load it into a buffer, then I just add the string to a list box. My problem is when I use Globalfree() to free up the memory. The error is as follows:

"Windows has triggered a breakpoint in gameTest.exe.
This may be due to a corruption of the heap, and indicates a bug in gameTest.exe or any of the DLLs it has loaded."

It's pretty standard, but here is the weird part: This error message only happens half the time. For example: if the user inputs "asdf" the code works without issue, but if the string they input is "bats" the error happens. I am not entirely sure as to how this is possible because if it works for one string it should work for any other. If anyone can lend me a hand as to why this is happening, let me know.

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
// Message handler for the control dialog
INT_PTR CALLBACK ControlDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
		case WM_INITDIALOG:
			return (INT_PTR)TRUE;

		case WM_COMMAND:
		{
			int dlgId = LOWORD(wParam);

			switch (dlgId)
			{
				case IDOK:
				{	
					int length = GetWindowTextLength(GetDlgItem(hDlg, IDC_EDIT));

					if (length > 0)
					{
						HGLOBAL hDlgBuf = GlobalAlloc(GHND, length + 1);
						LPWSTR buffer = (LPWSTR) hDlgBuf;

						GetDlgItemText(hDlg, IDC_EDIT, buffer, length + 1);
						SendDlgItemMessage(hDlg, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buffer);

						GlobalFree(hDlgBuf); // This is where it errors
						return (INT_PTR)TRUE;
					}
					else
					{
						MessageBox(hDlg, L"You didnt enter anything!", L"", MB_ICONINFORMATION | MB_OK);
						return (INT_PTR)TRUE;
					}
				}
					break;  // case IDOK
				case IDC_CLEAR:
					SendDlgItemMessage(hDlg, IDC_LIST, LB_RESETCONTENT, 0, 0);
					return (INT_PTR)TRUE;
					break;
				case IDCANCEL:
					EndDialog(hDlg, LOWORD(wParam));
					return (INT_PTR)TRUE;
					break;
			}
		}
			break;
	}
	return (INT_PTR)FALSE;
}


I just have the dialog box handler here, but let me know if you need to see my entire .cpp file, any other files I have linked in, or my logs. Thanks!

-George
From memory, GlobalAlloc returns a handle that you should pass to GlobalLock, which gives you a pointer you can use.

Going the other way, you call GlobalUnlock to release the block (yes I know in WIN32 it doesn't do anything), then GlobalFree.
Thanks for the advice, and it seemed to work; however, the strings that worked before like "asdf" now give me the error. It seems as if the strings that worked and the ones that didn't have switched with this new addition. Is there any other memory related code that I can use to make every string work?

-George
Are you using Unicode strings? If you are, the buffer is half as large as it needs to be.

You've used LPWSTR, which implies you are using Unicode strings.
Thanks kbw. I guess that I didn't put 2 and 2 together and realize that UNICODE characters are bigger. It works if I double my buffer.

-George
Topic archived. No new replies allowed.