+ -
当前位置:首页 → 问答吧 → WWWW.exe 中的 0x00bd7256 处未处理的异常: 0xC0000005: 读取位置 0x00000004 时发生访问冲突

WWWW.exe 中的 0x00bd7256 处未处理的异常: 0xC0000005: 读取位置 0x00000004 时发生访问冲突

时间:2011-11-29

来源:互联网

// WWWWView.cpp : CWWWWView 类的实现
//

#include "stdafx.h"
#include "WWWW.h"

#include "WWWWDoc.h"
#include "WWWWView.h"
#include "Myplane.h"
#include "Enemy.h"
#include "Ball.h"
#include "Bomb.h"
#include "EX.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CWWWWView

IMPLEMENT_DYNCREATE(CWWWWView, CView)

BEGIN_MESSAGE_MAP(CWWWWView, CView)
ON_WM_CREATE()
ON_WM_TIMER()
ON_WM_KEYDOWN()
ON_WM_KEYUP()
END_MESSAGE_MAP()

// CWWWWView 构造/析构

CWWWWView::CWWWWView()
{
// TODO: 在此处添加构造代码

}

CWWWWView::~CWWWWView()
{
}

BOOL CWWWWView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式

return CView::PreCreateWindow(cs);
}

// CWWWWView 绘制

void CWWWWView::OnDraw(CDC* pDC)
{
CWWWWDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;

// TODO: 在此处为本机数据添加绘制代码




Enemy::LoadImage();

Bomb::LoadImage();

EX::LoadImage();

  Ball::LoadImage();


  Myplane::LoadImage();
pMyplane=new Myplane;


}


// CWWWWView 诊断

#ifdef _DEBUG
void CWWWWView::AssertValid() const
{
CView::AssertValid();
}

void CWWWWView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}

CWWWWDoc* CWWWWView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWWWWDoc)));
return (CWWWWDoc*)m_pDocument;
}
#endif //_DEBUG


// CWWWWView 消息处理程序

int CWWWWView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: 在此添加您专用的创建代码
SetTimer(1,50,NULL);

return 0;
}

void CWWWWView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
RECT rc;
GetClientRect(&rc);

CDC *pDC=GetDC();
CBrush brush;
brush.CreateSolidBrush(RGB(3,108,254));
CBrush *oldbrush=pDC->SelectObject(&brush);
pDC->Rectangle(&rc);
pDC->SelectObject(oldbrush);

if(pMyplane!=NULL)

{
pMyplane->Draw(pDC,false);

}





POSITION p1=NULL,p2=NULL,p7=NULL,p8=NULL,p3=NULL,p4=NULL;


POSITION p9=NULL,p10=NULL;
  for(p9=ball.GetHeadPosition();(p10=p9)!=NULL;)
{
Ball *pBall=(Ball*)ball.GetNext(p9);




/
pBall->Draw(pDC,FALSE);




}



int random=rand()%20;//随机产生敌机数,并且有时延
if(random==1)
enemy.AddTail(new Enemy);//创建链表存储敌机

for(p1=enemy.GetHeadPosition();(p2=p1)!=NULL;)
{
Enemy *pEnemy=(Enemy*)enemy.GetNext(p1);
pEnemy->Draw(pDC,FALSE);



if(pEnemy->GetPoint().x+35<pMyplane->GetPoint().x||pMyplane->GetPoint().x+50<pEnemy->GetPoint().x)
{;}//区域控制就是当战机与敌机在同一x区域时敌机发射子弹其他区域不发射
//这控制的是边界以外的区域,执行空语句  
else
{int random=rand()%20;
//控制敌机产生子弹的数量,数据越大,等待时间越长,产生子弹数目越少
if(random==1)
//当随机数对取余等于时才发射子弹,其余不发射

ball.AddTail(new Ball(pEnemy->GetPoint().x+13,pEnemy->GetPoint().y,pEnemy->GetMontion()));
//因为子弹要获取每一家敌机的坐标,所以要放在这里产生子弹。子弹要产生在敌机中间。
//montion是为了使子弹方向明确

}

}





int n=0;
n=GetKey(VK_SPACE);/
if(n==1)
{
bomb.AddTail(new Bomb(pMyplane->GetPoint().x,pMyplane->GetPoint().y));
//取战机地址后在那个坐标添加一个bomb,存入链表
bomb.AddTail(new Bomb(pMyplane->GetPoint().x+40,pMyplane->GetPoint().y));
//两排导弹,横坐标不同,纵坐标一样

}

for(p3=bomb.GetHeadPosition();(p4=p3)!=NULL;)
{
Bomb *pBomb=(Bomb*)bomb.GetNext(p3);
pBomb->Draw(pDC,false);

}


////////////////////////////////导弹与敌机碰撞爆炸////////////////////////////////////////////////////////////////

for(p1=enemy.GetHeadPosition();( p2=p1)!=NULL;)
{
Enemy *pEnemy=(Enemy*)enemy.GetNext(p1);
  CRect rectEnemy=pEnemy->GetRect();

  for(p3=bomb.GetHeadPosition();(p4=p3)!=NULL;)
{
Bomb *pBomb=(Bomb*)bomb.GetNext(p3);
  CRect rectBomb=pBomb->GetRect();//炸弹区域
CRect rect;
if(rect.IntersectRect(rectEnemy,rectBomb))
{
ex.AddTail(new EX(rectEnemy.left,rectEnemy.top));
   
enemy.RemoveAt(p2);
delete pEnemy;
bomb.RemoveAt(p4);
delete pBomb;
break;

}
}
}
   

/////////////////////////////////////子弹与战机碰撞爆炸///////////////////////////////////////////////
//

 CRect rectMyplane=pMyplane->GetRect();

  for(p9=ball.GetHeadPosition();(p10=p9)!=NULL;)
{
Ball *pBall=(Ball*)ball.GetNext(p9);
  CRect rectBall=pBall->GetRect();
 


CRect rect1;
if(rect1.IntersectRect(rectMyplane,rectBall))
{
ex.AddTail(new EX(rectMyplane.left,rectMyplane.top));
   


delete pMyplane;
pMyplane=NULL;

ball.RemoveAt(p10);
delete pBall;
   
break;

}
}


  POSITION p5=NULL,p6=NULL;
  for(p5=ex.GetHeadPosition();(p6=p5)!=NULL;)

  {

  EX *pEX=(EX*)ex.GetNext(p5);
pEX->Draw(pDC,false);

  } 













CView::OnTimer(nIDEvent);
}

void CWWWWView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDC *pDC=GetDC();

if(pMyplane==NULL)
return;
///*/*/*/*/*/*/*/*/*/*/*控制战机方向*/*/*/*/*/*/*/*/*/*/*/
if(nChar==VK_RIGHT)
{
pMyplane->SetHorMotion(1);
}
if(nChar==VK_LEFT)
{
pMyplane->SetHorMotion(-1);
}
if(nChar==VK_UP)
{
pMyplane->SetVerMotion(1);
}
if(nChar==VK_DOWN)
{
pMyplane->SetVerMotion(-1);
}





CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CWWWWView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

CDC *pDC=GetDC();
if(nChar==VK_RIGHT)
{
pMyplane->SetHorMotion(0);
}

if(nChar==VK_LEFT)
{
pMyplane->SetHorMotion(0);
}

if(nChar==VK_UP)
{
pMyplane->SetVerMotion(0);
}
if(nChar==VK_DOWN)
{
pMyplane->SetVerMotion(0);
}




CView::OnKeyUp(nChar, nRepCnt, nFlags);
}
如果不加delete pMyplane;
pMyplane=NULL;就没有断点,加了就有了,我也知道可能是指针出问题了,就是不知道怎么解决。



这是我写的飞机大战的代码 ,初学者,碰到断点一直无法解决,求高手指点,麻烦你说的详细一点,小弟不胜感激!!!

作者: waike2011   发布时间: 2011-11-29

delete pMyplane;
pMyplane=NULL;

这时,pMyplane里面一定有资源没销毁吧。

作者: jennyvenus   发布时间: 2011-11-29