-
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:
http://cboard.cprogramming.com/showt...hlight=asserts
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
-
Worked fine on my computer. Windows 2000 Professional.
-
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.
-
I compiled it in MSVC6 and .NET and ran it under XP pro and it ran fine.
-
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.