Skip to main content
gamedev.net
🔒 Locked

CreateWindow memory leak?

Started by renman29 Aug 1, 2006 at 6:42 PM 6 replies 2.9k views
Original Post
renman29
renman29
My Crt leak checker doesn't find any leaks, however I get this with D3D_DEBUG_INFO: "This may be due to a corruption of the heap..." with a break as indicated with --> below:

//---------------------
// I N I T  W I N D O W
//---------------------
bool init_window(HWND &g_hwnd,HINSTANCE &g_instance) {
ULONG window_width, window_height;
WNDCLASS window_class;
DWORD style;
   ZeroMemory(&window_class, sizeof(WNDCLASS));
#ifdef WINDOWED_MODE
   window_class.style          = CS_HREDRAW | CS_VREDRAW;//CS_OWNDC;   
   window_class.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
#else
   window_class.style          = CS_OWNDC;
#endif
   window_class.cbClsExtra     = 0; window_class.cbWndExtra     = 0;        
   window_class.hInstance      = g_instance;
   window_class.hCursor = LoadCursor(NULL,IDC_ARROW);
   window_class.lpszMenuName   = NULL;
   window_class.lpszClassName  = g_wnd_class_name;   
   window_class.lpfnWndProc    = default_window_proc;
   if(!RegisterClass(&window_class)) FatalError(L"Error registering window class");
   window_width=GetSystemMetrics(SM_CXSCREEN); window_height=GetSystemMetrics(SM_CYSCREEN);    
#ifdef WINDOWED_MODE
   style=WS_OVERLAPPEDWINDOW;
#else
   style=WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE;
#endif
   g_hwnd=CreateWindow(g_wnd_class_name,g_app_name,style,
                      0,0,window_width,window_height,NULL,NULL,
                      g_instance,NULL);
   if(!g_hwnd) {Problem=L"Error opening window";return false;}
   ShowWindow(g_hwnd,SW_SHOW);UpdateWindow(g_hwnd);SetFocus(g_hwnd);
   return true;
/*--->BREAKS HERE--->*/  }


The program operates fine, but for some reason it breaks at the end of the init window function. Wierd. It does this even if I turn off my crt memory leak checks. Up to the call to create the window, the program hasn't done anything at all yet. g_hwnd and g_hinstance are globals and init_window is in a seperate module. It should be something simple I'm guessing/hoping..
Oluseyi
Oluseyi
Problem=L"Error opening window";

What type is Problem? If it's a pointer type, it may be a state change/allocation to a string literal thing that's getting flagged, not an actual memory leak.
Evil Steve
Evil Steve
What does your window proc look like? Something might be going on in there.
renman29
renman29
Thanks, yep, it's:

extern "C" LPCWSTR Problem=L"0 errors ";

Pointer.. Ya, that's probably it.
(I'm not really familiar with string methods)

Any advice about doing this is very welcome. :)
renman29
renman29
Oh, oops, my window procedure is:

}//InitWindow//W I N D O W  P R O CLRESULT CALLBACK default_window_proc(HWND hwnd,UINT msg,                                     WPARAM wparam,LPARAM lparam){      switch(msg)   {      case WM_PAINT:   render(); break;       case WM_CLOSE:   g_exit_app=true; PostQuitMessage(0); return 0;      case WM_DESTROY: g_exit_app=true; PostQuitMessage(0); return 0;}   return (DefWindowProc(hwnd,msg,wparam,lparam));}


I don't know if this means anything, but I also just noticed a second break for that ID when the program exits:
void __cdecl __crtExitProcess (        int status        ){        __crtCorExitProcess(status);        /*         * Either mscoree.dll isn't loaded,         * or CorExitProcess isn't exported from mscoree.dll,         * or CorExitProcess returned (should never happen).         * Just call ExitProcess.         */        ExitProcess(status);/*---->BREAK---->*/}
renman29
renman29
I found the bug. It was really stupid, I accidentally commented a release. Although I'm still wondering about string usage.

Is it actually ok for me to assign a string and keep reassigning it?
I've been doing this:

extern "C" LPCWSTR Problem=L"Bla Bla Bla";

And then in any random module I reassign Problem, ie:

Problem=L"Everything's ok now, but should I be using strings like this?"

I'd really like to do some fast concatenations, and make arrays of strings like: string Blabla[100]; Which would be 100 resizable strings. I'd like to output them to a file, maybe using fprintf.. Any suggestions would be cool.
I've looked around and found some interesting classes, but I'm not sure which one's are better or easier to use. What do you like using?

[Edited by - renman29 on August 5, 2006 4:30:25 PM]
Enigma
Enigma
Your string assignments look technically fine to me. As long as you're always assigning string literals you don't have to worry about memory management.

Try setting a watch on some of the addresses listed in the report to see what they are.
Quote:
Original post by renman29
I've also been looking around for a fast string class to use. Any recommendations are welcome. :)

std::string/std::wstring?

Σnigma
renman29
renman29
Thanks. :)
Oops, I modified my post just as you posted, but thanks - I didn't know about adding a watch based on the listed address.

I'll try using std:wstring, thanks. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.