vector subscript out of range in debug

Dear forum,

I recently converted my C++ projects from Visual Studio 2003 to Visual Studio 2005. In 2003 everything worked fine, but now I get the following errors when using a .lib in an executable program.

* When .lib is in debug mode and exe also in debug I get the following error:

"vector subscript out of range", .... \VC8\include\vector, line 756

Although this might seem strange to all of you I am 100% sure that my vector is NOT going out of range there!!

* When .lib is in debug mode and exe in release I get the following error:

"Runtime library has detected a fatal error"

* When .lib is in release mode and exe in debug I get:

"Unhandled exception in msvcr80d.dll"

* When .lib is in release mode and exe in debug mode:

IT WORKS FINE!

Can anyone give me a hint on what is happening here

edb



Answer this question

vector subscript out of range in debug

  • dandesro

    With these classes:

    testlib.h:

  • RainMan82

    I kind of have the same problem. It looks like VS2005 has more safty features than VS6 and VS2003. Especially, when we use multi-threaded model, we do not know what exact the code executed. So for example, we want to acces the last element in the vector and then erase it. But what happens is that the vector may erase the element before we access it since we have multi-threaded. Add lock or mutex might solve the problem. Hopefully, it helps.



  • Malik05

    Can anyone who missed this help

  • rjm1963

    Hi !!

    Osha, could you help me I have the same problem ::

    "vector subscript out of range", .... \VC8\include\vector, line 756

    and ("Strandard C++ library Out of Range",0) line 757

    Cheers.

    Nabyl.



  • Latham

    Hi,

    I cannot send the entire code, but here is some simplified test code to specify the problem:

    std::vector<myclass* > testvector;

    myclass* testclass = new myclass();

    testvector.push_back(testclass);

    The error pops-up here on the push_back(). I get the same error when I do an index call on a sortlike vector, which is in another lib (notice the & in the return type for GetVector... maybe this is the problem, I had some similar problems with calling eg. c_str() on a function which returns by ref..., this was in VS2003, but it works fine on linux):

    in a class in the .lib:

    void AddElement(myclass* a){testvector.push_back(a)}; // testvector similar to above

    std::vector<myclass* >& GetVector(){ return testvector; }

    myclass* GetElement(int a){return testvectorAngel};

    in the exe:

    classa testa;

    myclass* a = new myclass(...);

    testa.AddElement(a);

    testa.GetElement(0) // and do something with it: WORKS FINE

    testa.GetVector()[0] // and do something: CRASH. it crashes too when I do: std::vector<myclass*> b = testa.GetVector(); b[0] and do something with b[0]...

    Hope this code clarifies the problem This code works fine when using VS2003 and KDevelop (Linux)....

    The other was indead a typo, sorry. I know that for some reason it is not allowed to mix debug and release libs/exes, but I always wonder why. What if you want to use a precompiled .lib file which was compiled in release mode, for which you do not have the code and cannot generate a debug .lib.... How can you debug the program that uses this lib Never had problems with that before (but I must add, I worked under Linux for a long time...)


  • Mukhthar

    edb wrote:
    Dear forum,

    I recently converted my C++ projects from Visual Studio 2003 to Visual Studio 2005.

    When upgrading to Visual Studio 2005, it is important to make sure that NO trace of VS2003 can make its way into your VS2005 project, neither in your lib nor your exe.

    What I tend to do is completely clean out my source tree (every obj, every lib, every sbr, every cpp every h.) and download one afresh from the SourceSafe repository. Then I do a full rebuild of the solution.

    edb wrote:
    In 2003 everything worked fine, but now I get the following errors when using a .lib in an executable program.

    * When .lib is in debug mode and exe also in debug I get the following error:

    "vector subscript out of range", .... \VC8\include\vector, line 756

    Although this might seem strange to all of you I am 100% sure that my vector is NOT going out of range there!!

    I'd like to see the source code for myself before I make this conclusion. What do the local variables say in the debugger Reminder: the valid range for a vector index/iterator goes from 0 to size()-1. For an empty (zero sized) vector, no index or iterator is valid.

    edb wrote:
    * When .lib is in debug mode and exe in release I get the following error:

    "Runtime library has detected a fatal error"

    * When .lib is in release mode and exe in debug I get:

    "Unhandled exception in msvcr80d.dll"

    * When .lib is in release mode and exe in debug mode: (debug ! Hope that's a typo, and you meant exe and lib are both in release).

    IT WORKS FINE!

    Can anyone give me a hint on what is happening here

    This is a different problem. Note that you CANNOT mix release libs with debug exes (or vice versa). The only supported scenarios are that EVERYTHING is debug or EVERYTHING is release. Mixing a debug lib with a release exe is an unsupported scenario, and crashes and unhandled exceptions are just some of the symptoms you'll get.

    The same goes for mixing libs from different compilers and different versions of the same compiler.



  • Brandon Paddock MS

    I'm having exactely the same problem regarding the 'vector subscript out of range line 756' when popping back a vector after upgrading to VS2005. Pushing back still works perfectly. Searching the web I find many others with the same problem, but no explanation or way to fix it. This is purely in debug mode, no problem with mixing release mode or anything. This is definately not a out of range vector as VS2003 runs the same code without any problems.

    #include "fire.h"
    vector < fire > MYFIRE;

    In main I have this function

    for(unsigned int i = 0 ; i < MYFIRE.size(); ++i)
        {
            MYFIREIdea.DoPhysics(MYTIMER.getdelta_time());
            if(MYFIREIdea.Pos().X()>400)
                {
                    for(int j = i; j < MYFIRE.size(); ++j)
                        {
                            MYFIRE[MYFIRE.size()] = MYFIRE[j];///////////error here
                        }
                    MYFIRE.pop_back();///////////error here
                   
                }
    }

    And this is where I put things into the vector array

        case 32: //enter
                MYFIRE.push_back(fire());
                MYUSERSHIP.userfire(MYFIRE[MYFIRE.size()-1],userVel);
            break;

    Can anyone help me come across a way to fix this


  • vector subscript out of range in debug