我的程序出现问题,调试断言失败 [英] Problem in my program, debug assertion failed
本文介绍了我的程序出现问题,调试断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码正常,问题是当我单击图片(第一行的前三张图片)时,它说调试断言失败".令人困惑的.....
My code is ok, the problem is that it said "Debug assertion failed" when I clicked the pictures (the first three pictures of the first row ). confusing .....
#include <afx.h>
#include <stdio.h>
#include <windows.h>
#include <winuser.h>
#define WM_MYTIMER WM_USER+100
#ifndef _CGPSVIEW__H_
#define _CGPSVIEW__H_
static POINT pt;
static POINT pt1[2];
static int ms=0;
static int n=0;
static int m[2]={-1,-1};
static int k;
static int c,d;
HRGN hRgn[4][4];
class CGpsView
{
private:
DWORD m_dwDibSize;
unsigned char *m_pDib, *m_pDibBits;
BITMAPINFOHEADER *m_pBIH;
RGBQUAD *m_pPalette;
int m_nPaletteEntries;
public:
CGpsView();
~CGpsView();
public:
BOOL LoadBmp(char *pzFilename); //读取显示图像
BOOL DrawBmp(HDC hdc, int x, int y, int width, int height);
UINT m_width, m_height;
};
#endif
CGpsView::CGpsView()
{
m_pDib = NULL;
}
CGpsView::~CGpsView()
{
if(m_pDib)
{
delete [] m_pDib;
m_pDib = NULL;
}
}
BOOL CGpsView::LoadBmp(char *pzFilename)
{
CFile cf;
if(!cf.Open(pzFilename, CFile::modeRead))
return FALSE;
DWORD dwDibSize;
dwDibSize = cf.GetLength() - sizeof(BITMAPFILEHEADER);
unsigned char *pDib;
pDib = new unsigned char[dwDibSize];
if(pDib == NULL)
return FALSE;
BITMAPFILEHEADER BFH;
// 读取文件头和数据
try
{
if(cf.Read(&BFH, sizeof(BITMAPFILEHEADER))!=sizeof(BITMAPFILEHEADER)||
BFH.bfType!='MB'||
cf.Read(pDib, dwDibSize)!=dwDibSize)
{
delete [] pDib;
pDib = NULL;
return FALSE;
}
}
catch(CFileException *e)
{
e->Delete();
delete [] pDib;
pDib = NULL;
return FALSE;
}
if(m_pDib)
{
delete [] m_pDib;
m_pDib = NULL;
}
m_pDib = pDib;
m_dwDibSize = dwDibSize;
// BITMAPINFOHEADER and RGBQUAD 地址
m_pBIH = (BITMAPINFOHEADER*)m_pDib;
//m_pPalette = (RGBQUAD *) &m_pDib[sizeof(BITMAPINFOHEADER)];
// 宽 高
m_width = m_pBIH->biWidth;
m_height = m_pBIH->biHeight;
m_nPaletteEntries = 1 << m_pBIH->biBitCount;
if( m_pBIH->biBitCount > 8 )
m_nPaletteEntries = 0;
else if( m_pBIH->biClrUsed != 0 )
m_nPaletteEntries = m_pBIH->biClrUsed;
// m_pDibBits 地址
m_pDibBits =
&m_pDib[sizeof(BITMAPINFOHEADER)+
m_nPaletteEntries*sizeof(RGBQUAD)];
return TRUE;
}
BOOL CGpsView::DrawBmp(HDC hdc, int x, int y, int width, int height)
{
if(!m_pDib)
return FALSE;
//BitBlt(hdc, x, y, width, height, memdc, 0, 0, SRCCOPY);
StretchDIBits(hdc, x, y, width, height,
0, 0, m_width, m_height, m_pDibBits,
(BITMAPINFO *) m_pBIH, BI_RGB, SRCCOPY);
return TRUE;
}
CGpsView cgpsview1,cgpsview2;
CString szFileName00,szFileName01,szFileName02,
szFileName03,szFileName1[8],szFileName2[16];
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
VOID CALLBACK MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
int getNum(int arrNum[],int P);
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 ;
int arrNum[16];
int a[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int b[16];
int* p;
int i,j;
szFileName00="F:\\我的代码\\shishi5\\00.bmp";
szFileName01="F:\\我的代码\\shishi5\\01.bmp";
szFileName02="F:\\我的代码\\shishi5\\02.bmp";
szFileName03="F:\\我的代码\\shishi5\\03.bmp";
szFileName1[0]="F:\\我的代码\\shishi5\\1.bmp";
szFileName1[1]="F:\\我的代码\\shishi5\\2.bmp";
szFileName1[2]="F:\\我的代码\\shishi5\\3.bmp";
szFileName1[3]="F:\\我的代码\\shishi5\\4.bmp";
szFileName1[4]="F:\\我的代码\\shishi5\\5.bmp";
szFileName1[5]="F:\\我的代码\\shishi5\\6.bmp";
szFileName1[6]="F:\\我的代码\\shishi5\\7.bmp";
szFileName1[7]="F:\\我的代码\\shishi5\\8.bmp";
switch (message)
{
case WM_CREATE:
srand( (unsigned)time( NULL ) );
b[0]= rand()%16; //b[0]为0~15之间的随机数
p=&b[0];
for( c=1; c <16;c++)
{
b[c]=rand()%16;
for( d=0;d <c;d++) //如果跟前面有一样的就不要,重新取
{
if(b[c] == b[d])
{
c--;
break;
}
}
}
for( k=0;k<8;k++)
{
szFileName2[*p]=szFileName1[k];
szFileName2[*(p+1)]=szFileName1[k];
p=p+2;
}
InvalidateRect(hwnd,NULL,TRUE);
return 0 ;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
hRgn[i][j]=CreateRectRgn(j*500/3+10*(j+1),i*500/3,(j+1)*500/3+10*(j+1),(i+1)*500/3);
cgpsview1.LoadBmp((LPTSTR)(LPCTSTR)szFileName00);
cgpsview1.DrawBmp( hdc,j*500/3+10*(j+1),i*500/3,500/3,500/3);
}
EndPaint(hwnd,&ps);
return 0 ;
case WM_LBUTTONDOWN:
pt.x=LOWORD(lParam);
pt.y=HIWORD(lParam);
SetTimer(hwnd,WM_MYTIMER,500,MyTimerProc);
hdc = GetDC (hwnd) ;
ReleaseDC (hwnd, hdc) ;
break;
case WM_DESTROY:
KillTimer(hwnd,WM_MYTIMER);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
VOID CALLBACK MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
HDC hdc;
hdc=GetDC(hwnd);
int i=0,j=0,l;
for( ;!(PtInRegion(hRgn[i][j],pt.x,pt.y));j++)
{
if(j==3)
{
i++;
j=0;
}
}
l=4*i+j;
if(ms==0)
{
cgpsview1.LoadBmp((LPTSTR)(LPCTSTR)szFileName01);
cgpsview1.DrawBmp( hdc,j*500/3+10*(j+1),i*500/3,500/3,500/3);
}
if(ms==1)
{
cgpsview1.LoadBmp((LPTSTR)(LPCTSTR)szFileName02);
cgpsview1.DrawBmp( hdc,j*500/3+10*(j+1),i*500/3,500/3,500/3);
}
if(ms==2)
{
cgpsview1.LoadBmp((LPTSTR)(LPCTSTR)szFileName2[l]);
cgpsview1.DrawBmp( hdc,j*500/3+10*(j+1),i*500/3,500/3,500/3);
pt1[n].x=j*500/3+10*(j+1);
pt1[n].y=i*500/3;
m[n]=l;
n++;
}
if(ms==3)
{
KillTimer(hwnd,WM_MYTIMER);
ms=-1;
}
ms++;
if(m[0]!=-1&&m[1]!=-1)
{
int value1= m[n-2];
int value2 = m[n-1];
if(szFileName2[value1].Compare(szFileName2[value2])==0)
{
Sleep(500);
cgpsview1.LoadBmp((LPTSTR)(LPCTSTR)szFileName03);
cgpsview1.DrawBmp( hdc,pt1[n-2].x,pt1[n-2].y,500/3,500/3);
cgpsview1.DrawBmp( hdc,pt1[n-1].x,pt1[n-1].y,500/3,500/3);
m[0]=-1;
m[1]=-1;
n=0;
}
else
{
Sleep(500);
cgpsview1.LoadBmp((LPTSTR)(LPCTSTR)szFileName00);
cgpsview1.DrawBmp( hdc,pt1[n-2].x,pt1[n-2].y,500/3,500/3);
cgpsview1.DrawBmp( hdc,pt1[n-1].x,pt1[n-1].y,500/3,500/3);
m[0]=-1;
m[1]=-1;
n=0;
}
}
}
推荐答案
没有人会深入研究您的代码以找到如此大量的代码中的问题.尝试仅发布相关代码.
此外,当发生类似情况时,您的第一个反应应该是使用调试器来缩小问题的范围.与在论坛上提问相比,您得到的结果要快得多.
Nobody is going to dig into your code to find the problem in such amount of code. Try posting only relevant code.
Furthermore, when something like that happens, your first reflex should be to use your debugger to narrow down the problem. You''ll get much faster results than asking a question on a forum.
{ if(cf.Read(&BFH,sizeof(BITMAPFILEHEADER))!= sizeof(BITMAPFILEHEADER)||
BFH.bfType!=''MB''||
cf.Read(pDib,dwDibSize)!= dwDibSize)
{
删除[] pDib;
pDib = NULL;
返回FALSE;
}
}
在try中的BFH.bfType!=''MB''应该改为BFH.bfType!=''BM''
{ if(cf.Read(&BFH, sizeof(BITMAPFILEHEADER))!=sizeof(BITMAPFILEHEADER)||
BFH.bfType!=''MB''||
cf.Read(pDib, dwDibSize)!=dwDibSize)
{
delete [] pDib;
pDib = NULL;
return FALSE;
}
}
在 try 中的 BFH.bfType!=''MB'' 应该 改为 BFH.bfType!=''BM''
cgpsview1.LoadBmp((LPTSTR)(LPCTSTR)szFileName00) ;
您可以更改:
例如
cgpsview1.LoadBmp(szFileName00.c_str());
cgpsview1.LoadBmp((LPTSTR)(LPCTSTR)szFileName00);
u can change :
eg
cgpsview1.LoadBmp(szFileName00.c_str());
这篇关于我的程序出现问题,调试断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文