これをほぼ半日デバッグしようとしていましたが、問題を見つけることができませんでした。ほとんどのトラブルを引き起こしているものをこの方法である:std :: list問題の原因となる要素の削除
//[datamember]
std::list<Projectile*> m_Projectiles_l;
//[predicate]
bool removeDeads(Projectile* pProj) {
return !(pProj->isAlive());
}
//[the method I think might be causing the problem]
void ProjectileList::KillDeadProjectiles()
{
std::list<Projectile*>::iterator it;
it = std::remove_if(m_Projectiles_l.begin(), m_Projectiles_l.end(), &removeDeads);
if (it != m_Projectiles_l.end())
{
std::list<Projectile*>::iterator itDelete;
for (itDelete = it; itDelete != m_Projectiles_l.end(); ++itDelete) {
delete (*itDelete);
}
m_Projectiles_l.erase(it, m_Projectiles_l.end());
}
}
VS2010ブレークエラー:
Unhandled exception at 0x00389844 in PsychoBots.exe: 0xC0000005: Access violation reading location 0xfeeeff3a.
ブレーキングは、この行に私をもたらします:
void ProjectileList::DoPhysicsStuff(const InputState& refInputState)
{
KillDeadProjectiles();
std::list<Projectile*>::iterator it;
for (it = m_Projectiles_l.begin(); it != m_Projectiles_l.end(); ++it) {
/*[THIS line]*/(*it)->DoPhysicsStuff(refInputState);
}
}
私の調査結果:
It gives a problem when: there are more than 2 elements in the list, and a "projectile that has been added to the list earlier than a projectile that has been added later on" is getting removed with this method.
It gives no problems when: There is only one element in the list OR All the elements are getting removed at the same time.
誰にでもこれのエラー?
もっとコードが必要な場合は、コメントしてください。私は今は小さなサイズにしています。
イテレータを 'remove_if'の結果を超えて使用することはできません。それらを消去することはできますが、特定の状態になることは保証されていないため、アクセスしないでください。しかし、この時間を無駄にすることなく、生のポインタの使用をやめてください。 –
@KerrekSB私は「私はそれらを使うことが許されていないので、スマートポインタについてのコメントはしないでください」というコメントにほとんど追加しました;しかし、人々はそれについてコメントしたいと思っています。 – xcrypt