English 中文(简体)
定点:删除造成麻烦的要素
原标题:std::list deletion of elements causing trouble

I ve been trying to debug this for almost half a day now and I just can t seem to find the problem. Most likely what is causing the trouble is this method:

//[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 break error:

Unhandled exception at 0x00389844 in PsychoBots.exe: 0xC0000005: Access violation reading location 0xfeeeff3a.

Breaking brings me to this line:

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.

谁能看到这方面的错误?

If you need more code please comment, I tried to keep it small size for now.

最佳回答

您可以依赖通过<代码>remove_if返回的容器外的集装箱内容。 这意味着,如果你想在集装箱内管理动态记忆,你就不得不采取不同的做法。 容易储存的是共享的吸收器物体,而不是原材料。 那么,你就只能使用排泄物,所有东西都将清理。 否则,你需要仔细撰写删除的机制,而不是使用<代码>remove_if/code>。

问题回答

Read the reference of std::remove_if() carefully. (http://www.cplusplus.com/reference/algorithm/remove_if/)

The values in the range "it" to "m_Projectiles_l.end()" are still valid, but its values are unspecified. Most likely those values are unaltered, depending on the implementation.

这样,一个要素可以列入新名单,仍然处在旧名单的末尾。 删除这一内容将导致一个记忆例外。

你必须找到另一种办法,删除不再提及的内容。 考虑智能点。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签