Windows Programs Note Releasing Resources

This is a discussion on Windows Programs Note Releasing Resources within the Windows Programming forums, part of the Platform Specific Boards category; Hi its took me a while to realise but once my windows program has finished it does not release the ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    76

    Windows Programs Note Releasing Resources

    Hi its took me a while to realise but once my windows program has finished it does not release the resources it has been using. If I press control alt delete and then go to processes I find my application that should of been terminated using 98% of the resources and I was wondering why?
    Code:
    // This Program is designed to incorporate defining a windows class
    // Creating a window a message handler using custom icons, cursors
    // Also a choice of music files made possible by a message system
    
    #include <windows.h>
    #include <windowsx.h>
    #include "RESOURCES.H"
    
    HINSTANCE theInstance;
    
    LRESULT CALLBACK messageHandler (HWND theWin, UINT theMsg, WPARAM wparam, LPARAM lparam)
    {
    	PAINTSTRUCT ps;
    	HDC hdc;
    	switch (theMsg)
    	{
    	case WM_CREATE:
    		{
    			return 0;
    			break;
    		}
    	case WM_PAINT:
    		{
    			hdc = BeginPaint(theWin,&ps);
    			EndPaint(theWin,&ps);
    			return 0;
    			break;
    		}
    	case WM_DESTROY:
    		{
    			PlaySound(NULL,theInstance,SND_PURGE);
    			PostQuitMessage(0);
    			break;
    			return 0;
    		}
    	case WM_COMMAND:
    		{
    			switch (LOWORD(wparam))
    			{
    			case MENU_FILE_ID3:
    				{
    					PostQuitMessage(0);
    					return 0;
    					break;
    				}
    			case MENU_CURSOR_ID1:
    				{
    					HCURSOR theCursor;
    					theCursor = LoadCursor(theInstance, MAKEINTRESOURCE(CURSOR_ID1));
    					SetCursor(theCursor);
    					return 0;
    					break;
    				}
    			case MENU_CURSOR_ID2:
    				{
    					HCURSOR theCursor;
    					theCursor = LoadCursor(theInstance, MAKEINTRESOURCE(CURSOR_ID2));
    					SetCursor(theCursor);
    					return 0;
    					break;
    				}
    			case MENU_CURSOR_ID3:
    				{
    					HCURSOR theCursor;
    					theCursor = LoadCursor(theInstance, MAKEINTRESOURCE(CURSOR_ID3));
    					SetCursor(theCursor);
    					return 0;
    					break;
    				}
    			case MENU_SONG_ID1:
    				{
    					PlaySound(MAKEINTRESOURCE(WAVE_ID1), theInstance,
    						SND_RESOURCE | SND_LOOP | SND_ASYNC);
    					return 0;
    					break;
    				}
    			case MENU_SONG_ID2:
    				{
    					PlaySound(MAKEINTRESOURCE(WAVE_ID2), theInstance,
    						SND_RESOURCE | SND_LOOP | SND_ASYNC);
    					return 0;
    					break;
    				}
    			case MENU_SONG_ID3:
    				{
    					PlaySound(MAKEINTRESOURCE(WAVE_ID3), theInstance,
    						SND_RESOURCE | SND_LOOP | SND_ASYNC);
    					return 0;
    					break;
    				}
    			default:
    				{
    					break;
    				}
    			}
    		}
    	default:
    		{
    			break;
    		}
    	}
    
    	return (DefWindowProc(theWin,theMsg,wparam,lparam));
    }
    
    int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, 
    					int nShowCmd)
    {
    	theInstance = hInstance;
    	MSG theMsg;
    	HWND WindowsHandle;
    	WNDCLASSEX winclass;
    	winclass.cbSize = sizeof(WNDCLASSEX);
    	winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
    	winclass.cbClsExtra = 0;
    	winclass.cbWndExtra = 0;
    	winclass.hInstance = hInstance;
    	winclass.lpfnWndProc = messageHandler;
    	winclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(ICON_ID1));
    	winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    	winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	winclass.lpszMenuName = "MainMenu";
    	winclass.lpszClassName = "windowClass";
    	winclass.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(ICON_ID1));
    
    	if (!(RegisterClassEx(&winclass)))
    		return 0;
    
    	if (!(WindowsHandle = CreateWindowEx(NULL, "windowClass", "The Big One Window",
    		WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0,0, 400, 400, NULL, NULL, hInstance, NULL)))
    		return 0;
    
    	while (1)
    	{
    		if (PeekMessage(&theMsg,WindowsHandle, 0, 0, PM_REMOVE))
    		{
    			if (theMsg.message == WM_QUIT)
    				break;
    			TranslateMessage(&theMsg);
    			DispatchMessage(&theMsg);
    		}
    	}
    	return 0;
    }
    Ps. For some reason my cursor is not changing when the user asks for a different cursor on the menu bar. I have been told that the SetCursor function has to be maintained by yourself but I don't know how to do this. If someone could help with this too it would be very greatful.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Set the second parameter of PeekMessage to NULL.

    Handle WM_SETCURSOR and use SetCursor in that message handler to change the cursor to one you want.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,854
    Look at DestroyWindow() and Unregister Class() to free resources as the app closes.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Thanks a lot. I think i just saw the word hInstance in the function parameter and automatically set it to that. Also thanks for the DestroyWindow() and UnregisterClass() functions I'll look into those, and thanks for the details on the setCursor function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Large windows compilable programs
    By kawshik in forum C Programming
    Replies: 2
    Last Post: 07-02-2007, 06:07 AM
  2. Windows Programs With MSVC++ 2005
    By Brad0407 in forum Windows Programming
    Replies: 12
    Last Post: 04-22-2007, 02:46 PM
  3. FlashWindowEx not declared?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 05-17-2003, 02:58 AM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21