1

I looked at a few examples and as far as I can tell I'm using the splice method correctly. However, when I run the program in debug mode under Visual C++, the call stack shows my call to splice eventually reaches this line where it crashes:

_Mynextiter = _Parent_proxy->_Myfirstiter;

The code:

    for(std::list<Rect>::iterator i = rects.begin(); i != rects.end();)
    {
        if(i->isOverlapping(newRect))
        {
            Rect oldRect = (*i);
            i = rects.erase(i);
            std::list<Rect> & lr = oldRect.split(newRect);
            //doesn't work either
            //rects.splice(rects.begin(), lr, lr.begin(), lr.end());
            rects.splice(rects.begin(), lr);
        }
        else i++;
    }
stands2reason
  • 672
  • 2
  • 7
  • 18
  • Possibly related to http://stackoverflow.com/questions/143156/splice-on-stdlist-and-iterator-invalidation – James M Feb 14 '12 at 16:09
  • OK, so it looks like the lists may not be splice-able within a loop. I made an implementation that doesn't splice within the loop, but I wasn't able to test it since I'm away from my Windows machine and I'm getting totally different behavior in g++. – stands2reason Feb 14 '12 at 22:48

1 Answers1

1

It seems you are using std::list<T>::splice() the right way, if I refer to any manual pages on the internet.

At the contrary, I don't really like the reference in the line:

std::list<Rect> & lr = oldRect.split(newRect); 

I don't know what is doing Rect::split() but you should try to copy its result. Does that help?

yves Baumes
  • 8,836
  • 7
  • 45
  • 74
  • Sure enough, I was returning a reference to an object with automatic storage duration. I fixed this, and I also moved the splicing out of the loop. This seems to have fixed the error. – stands2reason Feb 16 '12 at 01:04