调试断言失败-矢量迭代器不可取消引用
Debug assertion failed - Vector iterator not dereferencable
运行以下代码时出现运行时错误:
void AlienShipManager::Update(float timeDelta,
BulletManager* bulletManager,
ParticleManager* particleManager,
GameStringSystem* stringBatch)
{
unsigned int i = 0;
while (i < m_alienShipList.size())
{
AlienResult result = m_alienShipList[i].Update(timeDelta, bulletManager);
switch (result)
{
case AlienResult::Dead:
break;
default:
break;
}
++i
}
}
在线
AlienResult result = m_alienShipList[i].Update(timeDelta, bulletManager);
下面是我如何将AlienShip添加到向量类:
m_alienShipList.push_back(AlienShip(position, speed, m_screeSize, m_alienShipTexture));
如果我有机会,错误也会出现:
AlienShip* newAlien = new AlienShip(position, speed, m_screeSize, m_alienShipTexture);
m_alienShipList.push_back(*newAlien);
delete newAlien;
但如果我将其更改为:,则不会出现
AlienShip* newAlien = new AlienShip(position, speed, m_screeSize, m_alienShipTexture);
m_alienShipList.push_back(*newAlien);
从而导致巨大的存储器泄漏。
这就是我的AlienShip类的外观:
#pragma once
#include "Body.h"
#include "BulletManager.h"
#include "ParticleManager.h"
enum AliensShipState
{
flying,
dying,
dead,
escaped
};
enum AlienResult
{
No,
Hit,
Dying,
Dead,
Escaped
};
class AlienShip : public Body
{
public:
AlienShip(void);
AlienShip(float2& position, float2& speed, float2* screenSize, ID3D11Texture2D* alienTexture);
~AlienShip(void);
AlienResult Update(float timeDelta, BulletManager* bulletManager);
void Draw(BasicSprites::SpriteBatch^ spriteBatch);
protected:
float m_baseY;
AliensShipState m_state;
float2* m_screenSize;
};
AlienShip类继承自Body类,Body类中有Sprite类,后者中有另一个向量。但由于Sprite类在其他地方运行得很好,我不认为它是错误的来源。
我想知道为什么会发生这种情况,因为我找不到删除临时对象和损坏向量迭代器之间的关系,如果它已经损坏了。
程序也在Release中编译和运行,但存在一些数据损坏。
我正在使用Visual Studio 2012 Beta for Windows 8。
如果你需要更多的源代码,请写。不幸的是,发布所有代码非常困难,因为这是一个复杂的程序。
考虑到按值将项添加到向量时它不起作用,但当泄漏指针时它起作用,我有95%的信心,AlienShip
的复制构造函数会执行浅复制,从而导致问题。
编辑:请注意,m_alienShipList.push_back(AlienShip(position, speed, m_screeSize, m_alienShipTexture));
会导致类的副本,如果副本构造函数工作不正常,稍后会导致问题。
事实上,如果您粘贴的AlienShip
定义是正确的,那么实际上只有默认的复制构造函数可能会做错误的事情(您有自己的析构函数这一事实进一步强化了这一点)。
要么实现一个执行深度复制的复制构造函数,要么更优选地重写您的类,使用RAII为您管理内存,以便默认副本是正确的。
相关文章:
- 为什么范围算法与 std 的迭代器不兼容?
- 矢量迭代器不能与 std::shared_ptr<> 取消引用
- forward_list迭代器不兼容
- 向量迭代器不兼容的错误,用于保存另一个向量的迭代器的向量
- C++,弹出调试断言失败窗口,我得到矢量迭代器不兼容的错误运行时
- f.getline() 迭代器不增加
- 当我发生碰撞检测时,矢量迭代器不兼容
- 为什么C++字符串迭代器不检查错误?
- debug断言矢量迭代器不可用find_if删除
- 为什么标准容器迭代器不会重载"->*"?
- 为什么反向迭代器不是 C++17 标准下的正式迭代器类别?
- C XTREE崩溃在出口上,映射/设置迭代器不可用
- C++:为什么输出迭代器不支持比较操作?
- 迭代器不兼容错误
- C++向量迭代器不是增量的
- 为什么更多的迭代器不是随机访问?
- 为什么迭代器不返回集合的开头
- 字符串迭代器不兼容错误,当我将字符串转换为矢量<byte>时
- 迭代器不能被另一个相同大小的向量重用吗?
- 字符串迭代器不可用C 问题