title: Crash when deleting a pointer in a loop and accessing the next element
body: <p>I have an array containing bullet class pointers and QList containing  QGraphicsItem class pointers ... Bullet derives from QGraphicsItem. I iterate through QList and check if QGrapjicsItem* is the same as one in Bullet* array.. if it is i delete item. The problem is, in the next iteration of the loop, when i want to acces next item in the QList - it crashes (segmentation fault) - someone knows what may be the reason of it?</p>
  <pre><code>void PlayerDefences::checkCollisions(QGraphicsItem* item)
  {
      QList&lt;QGraphicsItem*&gt; itemsCollideWithShip = item-&gt;collidingItems();  
      std::vector&lt;Bullet*&gt;* bullets = Level1::getBulletContainer();
      for (int i = 0; i &lt; itemsCollideWithShip.size(); i++) {
          try {
              if(itemsCollideWithShip[i]==nullptr) continue;
              if(Bullet* d = dynamic_cast&lt;Bullet*&gt;(itemsCollideWithShip[i]); d != nullptr ){  
                  bullets-&gt;erase(std::remove(bullets-&gt;begin(), bullets-&gt;end(), d), bullets-&gt;end());
                  Game::getScene()-&gt;removeItem(d);
                  delete d;
                  d = nullptr; 
              }
          } catch (const std::bad_cast&amp; e) {
              std::cout &lt;&lt; e.what() &lt;&lt; '\n';
   
          }
      }
  }
  </code></pre>
  <p>And this is my full stack trace dump:</p>
  <pre><code>1  libstdc++-6!.dynamic_cast                                                                                                                                            0x7ffa3b1402f2 
  2  PlayerDefences::checkCollisions                                                                                                               playerdefences.cpp 47  0x7ff7e0fb651b 
  3  Player::advance                                                                                                                               player.cpp         97  0x7ff7e0fb554c 
  4  Level1::advance                                                                                                                               level1.cpp         102 0x7ff7e0fb4532 
  5  QtPrivate::FunctorCall&lt;QtPrivate::IndexesList&lt;&gt;, QtPrivate::List&lt;&gt;, void, void (Level1:: *)()&gt;::call(void (Level1:: *)(), Level1 *, void * *) qobjectdefs_impl.h 152 0x7ff7e0fbcdb9 
  6  QtPrivate::FunctionPointer&lt;void (Level1:: *)()&gt;::call&lt;QtPrivate::List&lt;&gt;, void&gt;(void (Level1:: *)(), Level1 *, void * *)                       qobjectdefs_impl.h 185 0x7ff7e0fbda8c 
  7  QtPrivate::QSlotObject&lt;void (Level1:: *)(), QtPrivate::List&lt;&gt;, void&gt;::impl(int, QtPrivate::QSlotObjectBase *, QObject *, void * *, bool *)    qobjectdefs_impl.h 395 0x7ff7e0fbcfba 
  8  QMimeType::filterString() const                                                                                                                                      0x7ffa37bb9357 
  9  QTimer::timerEvent(QTimerEvent *)                                                                                                                                    0x7ffa37948a9a 
  10 QObject::event(QEvent *)                                                                                                                                             0x7ffa3792b3b7 
  11 QApplicationPrivate::notify_helper(QObject *, QEvent *)                                                                                                              0x7ffa36955946 
  12 QCoreApplication::sendEvent(QObject *, QEvent *)                                                                                                                     0x7ffa378f6ed8 
  13 QEventDispatcherWin32Private::sendTimerEvent(int)                                                                                                                    0x7ffa37a69f2f 
  14 QEventDispatcherWin32::event(QEvent *)                                                                                                                               0x7ffa37a6f700 
  15 QApplicationPrivate::notify_helper(QObject *, QEvent *)                                                                                                              0x7ffa36955946 
  16 QCoreApplication::notifyInternal2(QObject *, QEvent *)                                                                                                               0x7ffa378f69ed 
  17 QCoreApplicationPrivate::sendPostedEvents(QObject *, int, QThreadData *)                                                                                             0x7ffa378fa3a9 
  18 QWindowsGuiEventDispatcher::sendPostedEvents()                                                                                                                       0x7ffa37329e6e 
  19 QEventDispatcherWin32::processEvents(QFlags&lt;QEventLoop::ProcessEventsFlag&gt;)                                                                                          0x7ffa37a6e9f0 
  20 QWindowsGuiEventDispatcher::processEvents(QFlags&lt;QEventLoop::ProcessEventsFlag&gt;)                                                                                     0x7ffa37329e55 
  21 QEventLoop::exec(QFlags&lt;QEventLoop::ProcessEventsFlag&gt;)                                                                                                              0x7ffa37900fd3 
  22 QCoreApplication::exec()                                                                                                                                             0x7ffa378ff8b6 
  23 main                                                                                                                                          main.cpp           9   0x7ff7e0fb4c62 
  </code></pre>