Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 915691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:55:50+00:00 2026-05-15T17:55:50+00:00

Note that the order can go either way (erase first then push back, just

  • 0

Note that the order can go either way (erase first then push back, just that this way doesn’t require creating a local reference to the object).

  for ( GameObjItr gameObj = m_gameObjects.begin();
        gameObj != m_gameObjects.end(); gameObj++ ) {
    if ( *gameObj && *gameObj != gameObject ) {
      const sf::FloatRect &otherObj = ( *gameObj )->GetCollisionBox();
      if( mainObj.Intersects( otherObj ) ) {
        m_gameObjects.splice( m_gameObjects.end(), m_gameObjects, gameObj );
        return m_gameObjects.back();
      }
    }
  }

  return NULL;
}

The idea is that if the function finds an object colliding with the object passed, it adds that object to the end of the list, erases it from its current position, then returns the object. I don’t think iterator invalidation is a problem since I am returning immediately after the erasure?

The code causes the program to crash.

P.S. cppreference says that erase “deletes” the object, but I assumed that this just removed it from the list. (If this is wrong, how can I remove an element by its location. The remove function doc I’m reading only allows passing the value of the object).

Edit: The location of the crash isn’t always the same, but it occurs because of this code (i.e. if I revert it, acts normally). What causes a variably timed delayed crash? I do not want the object found to be destroyed.

GameObject* GameObjectManager::DetectCollision( const GameObject *
  gameObject ) {
  const sf::FloatRect &mainObj = gameObject->GetCollisionBox();

  for ( GameObjItr gameObj = m_gameObjects.begin();
        gameObj != m_gameObjects.end(); gameObj++ ) {
    if ( *gameObj && *gameObj != gameObject ) {
      const sf::FloatRect &otherObj = ( *gameObj )->GetCollisionBox();
      if( mainObj.Intersects( otherObj ) ) {
        m_gameObjects.splice( m_gameObjects.end(), m_gameObjects, gameObj ); //<--------
        return m_gameObjects.back(); //<-------------
      }
    }
  }

  return NULL;
}

Edit
Found the bug, when moving the object to the end of the list, caused infinite loop between 2 GameObjects colliding. Sorry, couldn’t have been deciphered from code posted.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:55:51+00:00Added an answer on May 15, 2026 at 5:55 pm

    When you say:

    m_gameObjects.erase( gameObj );
    

    the destructor for the thing contained in the list being erased (if it has one) will be called. It’s not clear from your question what the type of this thing is, or if this destructor call is expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note that all the code is a simplified example in order to only communicate
(note that this question is not about CAS, it's about the May fail spuriously
Note that this is for an ApiController in MVC 4 although I think it
Warning: Note that this is a dumb question of something that I would probably
[Please note that this is a different question from the already answered How to
This question is about a general technique in SQL, that I can't quite work
Note that the _src inherit IQueryable<U> and V inherit new() ; I wrote the
Note that I'm aware of other yield in vb.net questions here on SO. I'm
Edit : Note that, as Daniel and latkin noted in an answer and a
Please note that I'm not asking how but why. And I don't know if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.