저는 거의 반나절 동안 이것을 디버깅하려고 노력해 왔지만 문제를 찾을 수없는 것 같습니다.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