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 7021447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:27:03+00:00 2026-05-27T23:27:03+00:00

[EDIT 2] It’s probably because the surface is being freed while it’s still in

  • 0

[EDIT 2] It’s probably because the surface is being freed while it’s still in use by the other objects…So I want to write it so it’s only freed if no more of the object is on the screen.

[EDIT][UPDATE] I fixed the code to use the iterator, and I was still having the same issue…I just confirmed this to be an issue with the destructor for the object. In the class the destructor calls SDL_FreeSurface(image); to free the object’s image from memory…But objects created with the copy constructor apparently aren’t okay with that. What do I need to do to make it work correctly with objects created with the copy constructor? I can’t seem to find anything on Google pertaining to this.

Number refers to the number of bullets on the screen at a given time…I have a vector of the object “bullet” (vector bullet;)…The code checks if the trigger button (z) is down, and adds a new bullet to the vector if it is. Then it updates (moves, blits), then checks if the bullet is at the end of the screen…Everything works properly until a bullet reaches the end of the screen where it’s supposed to be destroyed.

if (trigger)
{
  bullet.push_back(Bullet(position.x, position.y));
  ++number;
}

for(int i = 0; i < bullet.size(); i++)
{
  bullet[i].update();
}

Below is where the issue is…When a bullet object is erased, the program exits with a segmentation fault…Now, I understand why this is happening since the vector size is changed while it’s in the loop, but I can’t figure out how to fix it…I added the break thinking that it would solve the problem since only one bullet can be <= 0 at a given time anyway, but it doesn’t…I initially had the if statement in the last for loop after the update function, but I put it in its own for loop so I could use the break.

for(int i = 0; i < bullet.size(); i++)  
{
  if (bullet[i].position.y <= 0)
  {     
    bullet.erase(bullet.begin() + i);
    --number;
    break;
  }
}

I’m new to vectors, so please bear with me here if I don’t understand something or if I’m making a n00b mistake.

  • 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-27T23:27:04+00:00Added an answer on May 27, 2026 at 11:27 pm

    The standard erase loop for sequence containers goes like this:

    for (std::vector<Bullet>::iterator it = v.begin(); it != v.end() /* not hoisted */; /* no increment */)
    {
        if (delete_condition)
        {
            it = v.erase(it);
        }
        else
        {
            ++it;
        }
    }
    

    The use of iterators is preferable over the use of a counting index since it frees your code from counter arithmetic which adds nothing to the clarity of the code and is rather much of a nuisance, while the iterator version here presented is fairly self-explanatory.

    The key is not to increment the loop variable in the event of an erase.

    You may also like to look into the remove/erase idiom:

    v.erase(std::remove_if(v.begin(), v.end(),
                           [](Bullet & b) -> bool { return delete_condition }),
            v.end());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: Prepare my objects for the use within a HashMap. after reading a bit
Edit: I'm looking for solution for this question now also with other programming languages.
Edit ok, great feedback here, got me pointed in the right direction. Use case
EDIT: When I merge link_def.cpp with link_dec.h, I only get the first error, not
Edit 1 begins The behaviour of the original post applies only to Windows XP
EDIT My jsfiddle entry is here : http://jsfiddle.net/ehNrE/3/ All the codes(only those required) below
Edit: Translated I have a RSS-feed that i want to parse. It's a podcast
Edit: This question was written in 2008, which was like 3 internet ages ago.
Edit: From another question I provided an answer that has links to a lot
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE

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.