Thread: memory leak

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your WndProc is badly designed.

    Firstly, you should not be calling DefWindowProc if you have handled the message yourself. It's only for messages that you do not handle.

    Secondly, and here's your real problem, WndProc is called many, many, many times, and each time it's called you read the files again. You never free this memory, so it eventually crashes.

    I can't tell you specifically how to fix it since your code is clearly not finished (you don't use the filenames anywhere).

    EDIT:
    Now that I think about it, you should move the whole directory reading part into the WM_CREATE handler and make arrFilename static. Free the memory in WM_DESTROY.
    Last edited by oogabooga; 09-17-2012 at 04:12 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    Quote Originally Posted by oogabooga View Post
    Your WndProc is badly designed.

    Firstly, you should not be calling DefWindowProc if you have handled the message yourself. It's only for messages that you do not handle.
    ok you say that I shouldn't use but I see it in Petzold book example

    Code:
    HELLOWIN.C
    
    /*------------------------------------------------------------
       HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
                     (c) Charles Petzold, 1998
      ------------------------------------------------------------*/
    
    #include <windows.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("HelloWin") ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASS     wndclass ;
    
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;
    
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
         hwnd = CreateWindow (szAppName,                  // window class name
                              TEXT ("The Hello Program"), // window caption
                              WS_OVERLAPPEDWINDOW,        // window style
                              CW_USEDEFAULT,              // initial x position
                              CW_USEDEFAULT,              // initial y position
                              CW_USEDEFAULT,              // initial x size
                              CW_USEDEFAULT,              // initial y size
                              NULL,                       // parent window handle
                              NULL,                       // window menu handle
                              hInstance,                  // program instance handle
                              NULL) ;                     // creation parameters
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
         
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         HDC         hdc ;
         PAINTSTRUCT ps ;
         RECT        rect ;
         
         switch (message)
         {
         case WM_CREATE:
              PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
              return 0 ;
    
         case WM_PAINT:
              hdc = BeginPaint (hwnd, &ps) ;
              
              GetClientRect (hwnd, &rect) ;
              
              DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
                        DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
              EndPaint (hwnd, &ps) ;
              return 0 ;
              
         case WM_DESTROY:
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by *nick View Post
    ok you say that I shouldn't use but I see it in Petzold book example
    I didn't say that you shouldn't use it; you have to use it. I said that you shouldn't call it if you've handled the message yourself. Notice that Petzold's example returns if it handles a message and only calles DefWindowProc if it doesn't handle the message. Basically you just have to change your breaks to return 0.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak
    By ssharish2005 in forum General Discussions
    Replies: 35
    Last Post: 07-05-2011, 11:10 PM
  2. Possible memory leak.
    By msh in forum C++ Programming
    Replies: 11
    Last Post: 07-21-2009, 12:28 PM
  3. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  4. Memory leak or no?
    By rafe in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2002, 04:33 PM
  5. Memory leak?
    By Weed4Me in forum C Programming
    Replies: 2
    Last Post: 11-09-2001, 05:47 PM