Jump to content
  • Advertisement
Sign in to follow this  
Medo Mex

Vector out of range

This topic is 1877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm rendering billboards using map:

 

<LPDIRECT3DTEXTURE9, vector<DWORD>*> mapTexture;

 

 

I'm iterating through the map to render billboards with the same texture so I can draw multiple billboard using the same texture with one draw call.

 

When the emitter die, I set m_Dead to true in the emitter class, now I want to remove the dead emitters from the particles manager.

 

So I use:

 

 

void ParticleManager::RemoveEmitter(DWORD EMITTER_ID)
{
    if (emitters.size() > EMITTER_ID)
    {
        vector<DWORD> *CurrentIDS = mapTexture[emitters[EMITTER_ID]->texture];
        if (CurrentIDS != NULL)
        {
             CurrentIDS->erase(std::remove(CurrentIDS->begin(), CurrentIDS->end(), EMITTER_ID), CurrentIDS->end());
             mapTexture[emitters[EMITTER_ID]->texture] = CurrentIDS;
        }

        emitters.erase(emitters.begin() + EMITTER_ID);
        }
}

I'm getting out of range exception.

Edited by Medo3337

Share this post


Link to post
Share on other sites
Advertisement
if (emitters.size() > EMITTER_ID)
You should use < instead of > on this line.

Share this post


Link to post
Share on other sites
My fault. I got confused because of the upper case and I'm not used to having the comparison in that order.

You could use a debugger and print the stack trace to see where the error takes place. That's probably the easiest way.

This line is not necessary.
mapTexture[emitters[EMITTER_ID]->texture] = CurrentIDS;

Share this post


Link to post
Share on other sites

If I changed this line:

 

CurrentIDS->erase(std::remove(CurrentIDS->begin(), CurrentIDS->end(), EMITTER_ID), CurrentIDS->end());

 

To:

CurrentIDS.clear();

I don't get out of range exception, but of course that will be a problem because I will be removing all the emitters, instead of the one that I want.

Share this post


Link to post
Share on other sites
When you erase an emitter all emitters stored after it in the vector will now be stored at another index (-1), so the indices that you store in the vectors will no longer be correct.

Share this post


Link to post
Share on other sites

Yes, I figured out that myself and resolved the problem :)

 

I had to use a pointer to the emitter instead of the emitter Id.

 

Thank you,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!