在debug下连接编译成功后,运行时弹出对话框:
skywar.exe 中的 0x004136c8 处最可能的异常: 0xC0000005: 读取位置 0x00000000 时发生访问冲突
源码长了点我分两个贴子
// skywar.h
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <stdlib.h>
#include <mmsystem.h>
#include <time.h>
#include <direct.h>
#include <ddraw.h>
#include <ddutil.h>
#include "resource.h"#define W_WIDTH 350
#define W_HEIGHT 450
#define TIMER_RUN 1 LPDIRECTDRAW7 pDD7;
LPDIRECTDRAWSURFACE7 pPSur;
LPDIRECTDRAWSURFACE7 pBBuf;
LPDIRECTDRAWSURFACE7 pOPlaPlane[3];
LPDIRECTDRAWSURFACE7 pOPlaBall;
LPDIRECTDRAWSURFACE7 pOPlaBomb;
LPDIRECTDRAWSURFACE7 pOPlaMap;
LPDIRECTDRAWSURFACE7 pOPlaBullet[2];
LPDIRECTDRAWSURFACE7 pOPlaFlame;
LPDIRECTDRAWSURFACE7 pOPlaDead;
LPDIRECTDRAWSURFACE7 pOPlaEnemy;
const int BULLETNUM=20;//子弹数
const int ENEMYNUM=10;//敌人数bool bActive =true;
char midipath[50];
char openmidi[50];
char closemidi[50];int speed=12;
int fps;
int gamestate;
#define INIT 0
#define START 1
#define OVER 2int forewidth,foreheight;
RECT backrect;
int backwidth,backheight;
struct Plane
{
int livenum;
POINT position;
bool ifsuper;
bool cancontrol;
int speed;
int pose;
POINT leftbulletpos[BULLETNUM];
POINT rightbulletpos[BULLETNUM];
int bullet;
};int enemylost;struct Enemy
{
int type;
POINT position;
int way;
int speed;
int dead;
};
POINT flamepos[ENEMYNUM];
int flamestatus[ENEMYNUM];POINT deadflamepos;
int deadflamestatus=0;//function
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
HWND InitWindow( HINSTANCE hInstance, int nCmdShow );
bool InitDDraw( void );
void FreeDDraw( void );
void Flip(void);
void DrawPlane(void);
HRESULT CreateBMP(int width,int height,char* filename,LPDIRECTDRAWSURFACE7 &tsurf);
void DrawMap(void );
void DrawText(void );
void UpdateFrame(void );
void DrawBullet(void);
void DrawFlame(void);
void DrawEnemy(void);
void CheckHit(void);
bool OutOfRange(POINT p);
void GetRect(RECT* rect, long left, long top, long width, long height);
//skywar.h end#include "skywar.h"Plane myplane;
Enemy enemy[ENEMYNUM];
HRESULT result;
HDC hdc,hdc1;
HWND hWnd;int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
hWnd=InitWindow(hInstance,nCmdShow);
if(hWnd==NULL)
return 0;
if(!InitDDraw())
{
MessageBox(GetActiveWindow(),"Init DirectDraw failed","error",MB_OK);
//FreeDDraw();
return 0;
}
ZeroMemory( &msg, sizeof(msg) );
while(1)
{
if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
{ //if the message is WM_QUIT return value is 0
if(!GetMessage(&msg,NULL,0,0))
return (int)msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if(bActive)
{
static int frame=0,nt,ot=0;
nt=GetTickCount();
if(nt>ot+1000)
{//more one second clear the frame
ot=nt;
fps=frame;
frame=0;
}
//
static int newcount,oldcount=0;
newcount=GetTickCount();
if(newcount>oldcount+speed)
{
frame++;
oldcount=newcount;
UpdateFrame();
}
}
else WaitMessage();
}
FreeDDraw();
}
//创建主窗口。
HWND InitWindow( HINSTANCE hInstance, int nCmdShow )
{
HWND hwnd;
WNDCLASS wc; //填充窗口类结构
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE(IDI_ICON1) );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "skywar"; //注册窗口类
RegisterClass( &wc );
//创建主窗口
hwnd = CreateWindowEx(
0,
"skywar",
"skywar",
WS_OVERLAPPEDWINDOW|WS_VISIBLE|WS_SYSMENU,
(GetSystemMetrics(SM_CXFULLSCREEN)-W_WIDTH)/2,
(GetSystemMetrics(SM_CYFULLSCREEN)-W_HEIGHT)/2,
W_WIDTH,
W_HEIGHT,
NULL,
NULL,
hInstance,
NULL ); if( !hwnd ) return NULL; //显示并更新窗口
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd ); return hwnd;
}LRESULT CALLBACK WinProc( HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam )
{
switch(message)
{
case WM_CREATE:
// do initialization stuff here
// return success
break;
case WM_ACTIVATE:
if( WA_INACTIVE == wParam )
bActive = false;
else
bActive = true;
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
PostMessage(hWnd,WM_CLOSE,0,0);
break;
case VK_ADD:
myplane.speed++;
if(myplane.speed>5) myplane.speed=5;
break;
case VK_SUBTRACT:
myplane.speed--;
if(myplane.speed<2) myplane.speed=2;
break;
case VK_RETURN:
if(gamestate!=START)
{
gamestate=START;
//init the value for start game
for(int i=0;i<ENEMYNUM;i++)
enemy[i].dead=0;
enemylost=0;
myplane.livenum=10;
myplane.speed=2;
myplane.pose=1;
myplane.bullet=0;
myplane.ifsuper=true;
myplane.cancontrol=false;
}break;
case VK_UP:
myplane.position.y=myplane.position.y-myplane.speed;
break;
case VK_DOWN:
myplane.position.y=myplane.position.y+myplane.speed;
break;
case VK_LEFT:
myplane.position.x=myplane.position.x-myplane.speed;
myplane.pose=0;
break;
case VK_RIGHT:
myplane.position.x=myplane.position.x+myplane.speed;
myplane.pose=2;
break;
}
case WM_DESTROY:
FreeDDraw();
char buf[256];
mciSendString( closemidi,buf,sizeof(buf),NULL);
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
skywar.exe 中的 0x004136c8 处最可能的异常: 0xC0000005: 读取位置 0x00000000 时发生访问冲突
源码长了点我分两个贴子
// skywar.h
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <stdlib.h>
#include <mmsystem.h>
#include <time.h>
#include <direct.h>
#include <ddraw.h>
#include <ddutil.h>
#include "resource.h"#define W_WIDTH 350
#define W_HEIGHT 450
#define TIMER_RUN 1 LPDIRECTDRAW7 pDD7;
LPDIRECTDRAWSURFACE7 pPSur;
LPDIRECTDRAWSURFACE7 pBBuf;
LPDIRECTDRAWSURFACE7 pOPlaPlane[3];
LPDIRECTDRAWSURFACE7 pOPlaBall;
LPDIRECTDRAWSURFACE7 pOPlaBomb;
LPDIRECTDRAWSURFACE7 pOPlaMap;
LPDIRECTDRAWSURFACE7 pOPlaBullet[2];
LPDIRECTDRAWSURFACE7 pOPlaFlame;
LPDIRECTDRAWSURFACE7 pOPlaDead;
LPDIRECTDRAWSURFACE7 pOPlaEnemy;
const int BULLETNUM=20;//子弹数
const int ENEMYNUM=10;//敌人数bool bActive =true;
char midipath[50];
char openmidi[50];
char closemidi[50];int speed=12;
int fps;
int gamestate;
#define INIT 0
#define START 1
#define OVER 2int forewidth,foreheight;
RECT backrect;
int backwidth,backheight;
struct Plane
{
int livenum;
POINT position;
bool ifsuper;
bool cancontrol;
int speed;
int pose;
POINT leftbulletpos[BULLETNUM];
POINT rightbulletpos[BULLETNUM];
int bullet;
};int enemylost;struct Enemy
{
int type;
POINT position;
int way;
int speed;
int dead;
};
POINT flamepos[ENEMYNUM];
int flamestatus[ENEMYNUM];POINT deadflamepos;
int deadflamestatus=0;//function
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
HWND InitWindow( HINSTANCE hInstance, int nCmdShow );
bool InitDDraw( void );
void FreeDDraw( void );
void Flip(void);
void DrawPlane(void);
HRESULT CreateBMP(int width,int height,char* filename,LPDIRECTDRAWSURFACE7 &tsurf);
void DrawMap(void );
void DrawText(void );
void UpdateFrame(void );
void DrawBullet(void);
void DrawFlame(void);
void DrawEnemy(void);
void CheckHit(void);
bool OutOfRange(POINT p);
void GetRect(RECT* rect, long left, long top, long width, long height);
//skywar.h end#include "skywar.h"Plane myplane;
Enemy enemy[ENEMYNUM];
HRESULT result;
HDC hdc,hdc1;
HWND hWnd;int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
hWnd=InitWindow(hInstance,nCmdShow);
if(hWnd==NULL)
return 0;
if(!InitDDraw())
{
MessageBox(GetActiveWindow(),"Init DirectDraw failed","error",MB_OK);
//FreeDDraw();
return 0;
}
ZeroMemory( &msg, sizeof(msg) );
while(1)
{
if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
{ //if the message is WM_QUIT return value is 0
if(!GetMessage(&msg,NULL,0,0))
return (int)msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if(bActive)
{
static int frame=0,nt,ot=0;
nt=GetTickCount();
if(nt>ot+1000)
{//more one second clear the frame
ot=nt;
fps=frame;
frame=0;
}
//
static int newcount,oldcount=0;
newcount=GetTickCount();
if(newcount>oldcount+speed)
{
frame++;
oldcount=newcount;
UpdateFrame();
}
}
else WaitMessage();
}
FreeDDraw();
}
//创建主窗口。
HWND InitWindow( HINSTANCE hInstance, int nCmdShow )
{
HWND hwnd;
WNDCLASS wc; //填充窗口类结构
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE(IDI_ICON1) );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "skywar"; //注册窗口类
RegisterClass( &wc );
//创建主窗口
hwnd = CreateWindowEx(
0,
"skywar",
"skywar",
WS_OVERLAPPEDWINDOW|WS_VISIBLE|WS_SYSMENU,
(GetSystemMetrics(SM_CXFULLSCREEN)-W_WIDTH)/2,
(GetSystemMetrics(SM_CYFULLSCREEN)-W_HEIGHT)/2,
W_WIDTH,
W_HEIGHT,
NULL,
NULL,
hInstance,
NULL ); if( !hwnd ) return NULL; //显示并更新窗口
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd ); return hwnd;
}LRESULT CALLBACK WinProc( HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam )
{
switch(message)
{
case WM_CREATE:
// do initialization stuff here
// return success
break;
case WM_ACTIVATE:
if( WA_INACTIVE == wParam )
bActive = false;
else
bActive = true;
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
PostMessage(hWnd,WM_CLOSE,0,0);
break;
case VK_ADD:
myplane.speed++;
if(myplane.speed>5) myplane.speed=5;
break;
case VK_SUBTRACT:
myplane.speed--;
if(myplane.speed<2) myplane.speed=2;
break;
case VK_RETURN:
if(gamestate!=START)
{
gamestate=START;
//init the value for start game
for(int i=0;i<ENEMYNUM;i++)
enemy[i].dead=0;
enemylost=0;
myplane.livenum=10;
myplane.speed=2;
myplane.pose=1;
myplane.bullet=0;
myplane.ifsuper=true;
myplane.cancontrol=false;
}break;
case VK_UP:
myplane.position.y=myplane.position.y-myplane.speed;
break;
case VK_DOWN:
myplane.position.y=myplane.position.y+myplane.speed;
break;
case VK_LEFT:
myplane.position.x=myplane.position.x-myplane.speed;
myplane.pose=0;
break;
case VK_RIGHT:
myplane.position.x=myplane.position.x+myplane.speed;
myplane.pose=2;
break;
}
case WM_DESTROY:
FreeDDraw();
char buf[256];
mciSendString( closemidi,buf,sizeof(buf),NULL);
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
将程序发到:[email protected]我看看
http://community.csdn.net/Expert/topic/3894/3894637.xml?temp=.8733332
接二:
http://community.csdn.net/Expert/topic/3894/3894647.xml?temp=.8129541
最后:
http://community.csdn.net/Expert/topic/3894/3894653.xml?temp=.4808924
x004136c8,结合.Map文件,查找所对应的源代码。
也就是说是访问pBBuf的时候出问题了,但不知道怎么去查了