Debug assertion failed

This is a discussion on Debug assertion failed within the Windows Programming forums, part of the Platform Specific Boards category; This program example from my Nitty Gritty book compiles without errors or warnings but when you click on the SYSTEM_FONT ...

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Debug assertion failed

    This program example from my Nitty Gritty book compiles without errors or warnings but when you click on the SYSTEM_FONT button I get this error:

    Debug Assertion Failed!
    Line 47
    Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
    For info see asserts.



    So I looked up asserts in one of my C++ books and it says:
    " A macro that stringizes and displays as an error message the expression passed in as a parameter if that expression evaluates to false."

    I did a search on this site and got 9 hits including this link that seems closely related but doesn't seem to help me:

    Ask about Debug Assert Failed

    Seeing line 47 in the error made me double check it but according to my book this is correct. So somewhere I must be 'passing in a false condition' but I do

    not see an error.




    Please shed some light on this for me!



    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    
    HINSTANCE hInstGlobal;
    HWND hEdit;
    
    int APIENTRY WinMain(HINSTANCE hInstance,
    			  HINSTANCE hPrevInstance,
    			  LPSTR lpCmdLine,
    			  int nCmdShow)			// nShowCmd???
    {
    	hInstGlobal = hInstance;
    
    	WNDCLASS WndClass;
    	WndClass.style = 0;
    	WndClass.cbClsExtra = 0;
    	WndClass.cbWndExtra = 0;
    	WndClass.lpfnWndProc = WndProc;
    	WndClass.hInstance = hInstance;
    	WndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
    	WndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
    	WndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    	WndClass.lpszMenuName = 0;
    	WndClass.lpszClassName = "WinProg";
    
    	RegisterClass(&WndClass);
    
    	HWND hWindow;
    	hWindow = CreateWindow("WinProg", "Edit Field",
    		WS_OVERLAPPED,
    		0,0,640,400,NULL,NULL,
    		hInstance, NULL);
    
    	ShowWindow (hWindow, nCmdShow);
    
    	UpdateWindow (hWindow);
    
    	MSG Message;
    	while (GetMessage(&Message, NULL, 0, 0))
    	{
    		DispatchMessage(&Message);
    	}
    
    	return (Message.wParam);
    }
    
    LRESULT CALLBACK WndProc (HWND hWnd, UINT uiMessage, WPARAM wParam, LPARAM lParam)
    {
    	switch(uiMessage)
    	{
    	case WM_CREATE:
    		HWND hButton, hButtonExit; 
    		hButton = CreateWindow("BUTTON","SYSTEM_FONT",
    			WS_CHILD | WS_VISIBLE |BS_PUSHBUTTON,		// BS_PUSHBUTTON returns BN_CLICKED			
    			10,320,140,20,
    			hWnd, (HMENU) 1,
    
    			hInstGlobal, NULL);
    		hButtonExit = CreateWindow("BUTTON","EXIT",
    			WS_CHILD | WS_VISIBLE |BS_PUSHBUTTON,
    			160,320,140,20,
    			hWnd, (HMENU) 2,
    			hInstGlobal, NULL);
    		hEdit = CreateWindow("EDIT","SYSTEM_FONT",
    			WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
    			10,340,120,20,
    			hWnd, (HMENU) 1,
    			hInstGlobal, NULL);
    		return 0;
    
    	case WM_COMMAND:
    	if(HIWORD(wParam) == BN_CLICKED)		// BN = Button Notification.  BS_PUSHBUTTON returns BN_CLICKED
    		{
    			if(LOWORD(wParam) == 1)
    			{
    				HDC hdc;
    				hdc = GetDC (GetParent((HWND) lParam));
    				HFONT hFont;
    				hFont = (HFONT) GetStockObject(SYSTEM_FONT);
    				
    				char *string1;
    				string1 = new char[255];
    				SendMessage (hEdit, WM_GETTEXT, 256,(LPARAM) string1);
    				bool FontDa;
    				FontDa =0;
    				if (lstrcmp (string1,"SYSTEM_FONT") == 0)
    				{
    					hFont = (HFONT) GetStockObject(SYSTEM_FONT);
    					FontDa = 1;
    				}
    				if (lstrcmp (string1,"SYSTEM_FIXED_FONT") == 0)
    				{
    					hFont = (HFONT) GetStockObject(SYSTEM_FIXED_FONT);
    					FontDa = 1;
    				}
    				if (lstrcmp (string1,"OEM_FIXED_FONT") == 0)
    				{
    					hFont = (HFONT) GetStockObject(OEM_FIXED_FONT);
    					FontDa = 1;
    				}
    				if (lstrcmp (string1,"ANSI_VAR_FONT") == 0)
    				{
    					hFont = (HFONT) GetStockObject(ANSI_VAR_FONT);
    					FontDa = 1;
    				}
    				if (lstrcmp (string1,"ANSI_FIXED_FONT") == 0)
    				{
    					hFont = (HFONT) GetStockObject(ANSI_FIXED_FONT);
    					FontDa = 1;
    				}
    				if (lstrcmp (string1,"DEFAULT_GUI_FONT") == 0)
    				{
    					hFont = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
    					FontDa = 1;
    				}
    				if (FontDa == 0)
    				{
    					hFont = (HFONT) GetStockObject(SYSTEM_FONT);
    					lstrcpy (string1, "SYSTEM_FONT");
    				}
    				SelectObject (hdc, hFont);
    				RECT rect;
    				SetRect (&rect, 0,0,480,280);
    				HBRUSH hBrush;
    				hBrush = CreateSolidBrush(RGB(255,255,255));
    				FillRect (hdc, &rect, hBrush);
    				TextOut (hdc, 340,10,string1, lstrlen(string1));
    				delete [] string1;
    				TEXTMETRIC tm;
    				GetTextMetrics (hdc, &tm);
    				int i;
    				int ix,iy;
    				ix = 0;
    				iy = 0;
    				char *string;
    				string = new char[2];
    				string[1] = 0;
    				for(i=0;i<=255;i++)
    				{
    					string[0] = i;
    					TextOut (hdc, ix, iy, string, lstrlen(string));
    					iy = iy + tm.tmHeight;
    					if ((iy/tm.tmHeight)==16)
    					{
    						iy = 0;
    						ix = ix + 20;
    					}
    				}
    
    
    				ReleaseDC (GetParent((HWND) lParam), hdc);
    				delete [] string1;
    			}
    			if (LOWORD(wParam) == 2)
    			{
    				SendMessage (GetParent((HWND) lParam),	// hWnd is the handle of a window object to which the message is sent.
    					WM_DESTROY ,0 ,0);					// Msg is the value which identifies the message.
    			}											// wParam is the first parameter of the 
    
    message.
    		}												// lParam is the second parameter of the 
    
    message.
    		return 0;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	default:
    	
    		return DefWindowProc (hWnd, uiMessage, wParam, lParam);
    	}
    }
    Compiling...
    Edit Field.cpp
    Linking...

    Edit Field.exe - 0 error(s), 0 warning(s)

    /code

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Worked fine on my computer. Windows 2000 Professional.

  3. #3
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    XP Problem then?

    I'm running Win XP Pro with MS VC++ 6.0.

    I suppose this could be a XP problem? When I get back home I could load Visual Studio on my desktop(Win2k Pro and Win 98) and see how it runs there.

    Thanks for trying this on your system MrWizard.

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    I compiled it in MSVC6 and .NET and ran it under XP pro and it ran fine.

  5. #5
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Talking I found a work around.

    I just ran it under compatibility mode for Win2k and it works great I'll have to remember this for future reference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Assertion Failed!
    By IndioDoido in forum C Programming
    Replies: 31
    Last Post: 03-25-2008, 11:07 AM
  2. debug assertion failed!
    By chintugavali in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2007, 03:05 AM
  3. Visual Studio 2005 Debug Assertion Failed
    By natmas in forum C++ Programming
    Replies: 7
    Last Post: 07-17-2007, 04:28 PM
  4. debug assertion failed !
    By blue_gene in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2004, 11:23 AM
  5. debug assertion failed?!?
    By ichijoji in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 03:23 PM

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