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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:53:39+00:00 2026-05-31T13:53:39+00:00

I have a strange problem here. I’m trying to erase an iterator from a

  • 0

I have a strange problem here. I’m trying to erase an iterator from a particle system (std::list). I erase an iterator when the particles goes out of screen. When I checked the size of the particles in debug mode, I found that they are the double compared to release mode, I don’t know why.

Kindly find the following code

 void ParticleManager::update(std::vector<ci::Vec2f> masses)
    {
        int targetDifference = masses.size() - m_Targets.size();

        if (masses.size() == 1 && targetDifference == 1)
        {
            addTarget();
        }
        else if (targetDifference > 1)
        {
            addTarget();
        } 
        else if (targetDifference < 0)
        {
            deleteTarget();
        }

        Vec2f currVec, offset;
        float fCurrLengthSquared;
        bool bAllTargetsActive = true;

        std::list<Particle>::iterator p = m_Particles.begin();
        while( p != m_Particles.end() ) 
        {
            p->update();

            float fMinSquaredLength = 0.0f;

            // influence of the masses
            for( int i = 0; i < (int) masses.size(); ++i )
            {
                currVec = masses[i] - p->m_Position;
                fCurrLengthSquared = currVec.lengthSquared();

                if (fCurrLengthSquared < ParamMgr.m_fForceMinDist * ParamMgr.m_fForceMinDist)
                {
                    fCurrLengthSquared = ParamMgr.m_fForceMinDist * ParamMgr.m_fForceMinDist;
                }

                if(fCurrLengthSquared < ParamMgr.m_fForceMaxDist * ParamMgr.m_fForceMaxDist)
                {
                    offset = currVec.normalized() / (fCurrLengthSquared / 500.0f);  // 1000.0f      
                    p->m_Direction += offset * ((float) TimerMgr.getDeltaTime() * ParamMgr.m_fGravity * ParamMgr.m_fGravity );
                }

                /*if( i == 0 )
                    fMinSquaredLength = fCurrLengthSquared;

                if( fCurrLengthSquared < fMinSquaredLength )
                    fMinSquaredLength = fCurrLengthSquared;*/
            }

            if( masses.size() > 0 )
            {
                float fSquareColorRadius = ParamMgr.m_fColorRadius * ParamMgr.m_fColorRadius;
                if( fMinSquaredLength > fSquareColorRadius )
                fMinSquaredLength = fSquareColorRadius;

                float fIntensity = 1.0f - (fMinSquaredLength / fSquareColorRadius) * 0.9f;
                //p->m_Color = ci::Color(0.0f, fIntensity, 0.0f);
                //p->m_Color = ci::Color(0.0f, 1.0f, 0.0f);
            }

            p->m_fAge += (float) TimerMgr.getDeltaTime();

            // outside the window
            if( p->m_Position.x < 0.0f - m_fCollisionOffsetX || p->m_Position.x > getWindowWidth() + m_fCollisionOffsetX ||
                p->m_Position.y < 0.0f - m_fCollisionOffsetY || p->m_Position.y > getWindowHeight() + m_fCollisionOffsetY )
            {
                p->m_bIsDead = true;
            }
            else
            { 
                // check targets
                for( std::list<Target>::iterator t = m_Targets.begin(); t != m_Targets.end(); t++ )
                {
                    if( t->checkParticle( p->m_Position ) )
                    {
                        p->m_ColorChange = Color( CM_HSV, t->m_fHue, t->m_fSat * 0.9f, 1.0f );
                        if( p->checkTarget( *t ) )
                        {
                            t->addParticleHit();
                        }
                    }
                    else
                    {
                        if( t != m_Targets.end() )
                        {
                            p->m_Targets.remove( *t );
                            //std::cout << "Try to erase: " << (*t).m_Position << " from target list." << std::endl;
                        }
                    }
                }
            }   

            if( p->m_bIsDead )
            {
                p = m_Particles.erase(p);

            }   
                p++;
}
  • 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-31T13:53:40+00:00Added an answer on May 31, 2026 at 1:53 pm

    Two points:

    1. You should use p->m_Targets.erase(t); rather than p->m_Targets.remove(*t). In particular, since you’re using remove(), which takes a value, the std::list class must scan through the entire list to find the value you specify.

    2. Removing an element from a list invalidates any iterators that point at that element. So your t iterator is getting invalidated. Using an invalid iterator has undefined behavior. Luckily, it’s (relatively) easy to solve. Try something like this:

      std::list<Target>::iterator t = m_Targets.begin(); 
      while (t != m_Targets.end())
      {
          if( t->checkParticle( p->m_Position ) )
          {
              p->m_ColorChange = Color( CM_HSV, t->m_fHue, t->m_fSat * 0.9f, 1.0f );
              if( p->checkTarget( *t ) )
              {
                  t->addParticleHit();
              }
              ++t;
          }
          else
          {
              t = p->m_Targets.erase(t);
          }
      }
      

    Note that we only do “++t” if we don’t remove an element (and not on every iteration through the loop, as you did with your for loop).

    There’s a similar bug in your outer loop. Replace this:

            if( p->m_bIsDead )
            {
                p = m_Particles.erase(p);
            }   
            p++;
    

    with this:

            if( p->m_bIsDead )
                p = m_Particles.erase(p);
            else
                ++p;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange problem, here is a quick snapshot of the system: I
I'm having a strange problem here... I have an ASP.NET 3.5 application that has
I have a strange problem with mod_rewrite, the rules that are relevant here are:
I've got a strange problem here. I have a winforms panel which programatically has
I have a strange problem in my rich client application. Here is some background:
I have a strange problem which I have been trying to fix for some
I have a strange problem here, I want to create an emtpy trigger: SET
I have one of our projects very strange problem here on http://konyak.georates.net/page-2/ we have
I have a strange problem with my Android app. When I start it from
i have a strange problem with IE8 installed in xp. i was trying to

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.