Bubbles的声明如下:
vector< CBubble* > Bubbles;
有一般代码如下:
if( row == (Rows - 1) )
{//结束游戏
delete Current;
State = GAME_ENDED;
vector< CBubble* >::iterator itr = Bubbles.begin();
for( ; itr != Bubbles.end(); ++itr)
{
point2d pt;
CBubble* pB = *itr;
pB->GetPos( pt );
pB->Launch();
delete *itr;//这句有问题。
}
Bubbles.clear();
Game->Reset();
return;
}
如果将delete *itr;这句删除,则程序执行正常,加上这句的话,进行过程中非法退出,没有任何提示,各位高手看看,是什么问题?
vector< CBubble* > Bubbles;
有一般代码如下:
if( row == (Rows - 1) )
{//结束游戏
delete Current;
State = GAME_ENDED;
vector< CBubble* >::iterator itr = Bubbles.begin();
for( ; itr != Bubbles.end(); ++itr)
{
point2d pt;
CBubble* pB = *itr;
pB->GetPos( pt );
pB->Launch();
delete *itr;//这句有问题。
}
Bubbles.clear();
Game->Reset();
return;
}
如果将delete *itr;这句删除,则程序执行正常,加上这句的话,进行过程中非法退出,没有任何提示,各位高手看看,是什么问题?
itr不是new出来的啊!为什么要用delete
有一般代码如下:
if( row == (Rows - 1) )
{//结束游戏
delete Current;
State = GAME_ENDED;
// vector< CBubble* >::iterator itr = Bubbles.begin();
// for( ; itr != Bubbles.end(); ++itr)
vector< CBubble* >::iterator itr = Bubbles.end();
for( ; itr != Bubbles.begin(); --itr) {
point2d pt;
CBubble* pB = *itr;
pB->GetPos( pt );
pB->Launch();
delete *itr;//这句有问题。
}
Bubbles.clear();
Game->Reset();
return;
}
----------------------------
#include <stdio.h>
#include <vector>
using namespace std;class CBubble
{
public:
CBubble(int a)
{
m_a=a;
printf("Constructor for %d called\n",m_a);
};
~CBubble()
{
printf("Desturctor for %d called\n",m_a);
};
void ShowValue()
{
printf("Value is:%d\n",m_a);
}
int m_a;
double m_b;
};void main()
{
vector< CBubble* > Bubbles;
//Initialze.....
Bubbles.push_back(new CBubble(1));
Bubbles.push_back(new CBubble(2));
Bubbles.push_back(new CBubble(3));
//Destuctor
vector< CBubble* >::iterator itr = Bubbles.begin();
for( ; itr != Bubbles.end(); ++itr)
{
CBubble* pB = *itr;
pB->ShowValue();
delete *itr;//这句有问题。
}
Bubbles.clear();}
-------------------------------------------------
There is no debug error,So I think the problem lies in the
"
pB->GetPos( pt );
pB->Launch();
"
Make sure that your have cleaned up all the staffs before deleting the objects.