WTF!!! Rendering System Broken

Everything about development and the OpenMW source code.
Chris
Posts: 1626
Joined: 04 Sep 2011, 08:33

Re: WTF!!! Rendering System Broken

Post by Chris »

jbo_85 wrote:There's a bug in Actors::removeCell() in actors.cpp regarding the iterator usage there. You need to change the code to something like

Code: Select all

    for(std::map<MWWorld::Ptr, Animation*>::iterator iter = mAllActors.begin(); iter != mAllActors.end();)
    {
      if(iter->first.getCell() == store){
        delete iter->second;
        mAllActors.erase(iter++);
      } else {
        ++iter;
      }
    }
to not invalidate the iterator. The incorrect code caused a crash on cell-change in gdb but wasn't crashing without a debugger attached.
Shouldn't that be

Code: Select all

iter = mAllActors.erase(iter);
? Existing iterators may not remain valid after erase/insert/etc, and erase() returns the next iterator after the one that was removed.
User avatar
Zini
Posts: 5538
Joined: 06 Aug 2011, 15:16

Re: WTF!!! Rendering System Broken

Post by Zini »

Nope. The iterator is incremented before erase is called. All good.
Yacoby
Posts: 119
Joined: 30 Sep 2011, 09:42

Re: WTF!!! Rendering System Broken

Post by Yacoby »

Zini wrote:Nope. The iterator is incremented before erase is called. All good.
I was like what? No. And then I double checked where the sequence point is. That is really really subtle.
User avatar
Zini
Posts: 5538
Joined: 06 Aug 2011, 15:16

Re: WTF!!! Rendering System Broken

Post by Zini »

I love my C++ ;)
User avatar
raevol
Posts: 3093
Joined: 07 Aug 2011, 01:12
Location: Caldera

Re: WTF!!! Rendering System Broken

Post by raevol »

Sounds like a good candidate for some quality code commenting. :)
User avatar
Zini
Posts: 5538
Joined: 06 Aug 2011, 15:16

Re: WTF!!! Rendering System Broken

Post by Zini »

Actually, that is more or less a standard technique. And if you have seen it once, it's pretty obvious.
Yacoby
Posts: 119
Joined: 30 Sep 2011, 09:42

Re: WTF!!! Rendering System Broken

Post by Yacoby »

Yup. It is just a bit counter intuitive on first sight (i.e. it doesn't work as "expected").
User avatar
psi29a
Posts: 5361
Joined: 29 Sep 2011, 10:13
Location: Belgium
Gitlab profile: https://gitlab.com/psi29a/
Contact:

Re: WTF!!! Rendering System Broken

Post by psi29a »

Inline code execution, it is like the 'order of operations' in code.
Chris
Posts: 1626
Joined: 04 Sep 2011, 08:33

Re: WTF!!! Rendering System Broken

Post by Chris »

Zini wrote:Nope. The iterator is incremented before erase is called. All good.
Hmm, I always worked under the assumption that all preexisting iterators become invalid after the number of elements in a container changes, regardless of what element it references.

Looking at some online C++ references, it seems different container types have different rules about what iterators become invalid after an erase/insert... which I think kinda reenforces the point. E.g., if the map is later changed to a vector, that iterator will potentially become invalid (depending on implementation details), but it can be an intermittent, hard-to-track error. If it's changed to another container type, who knows how this code will react.

Just my thoughts on the matter, anyway. You're right, though, that it seems the current code is valid (for C++11 at least, anyway).
Post Reply