vector of pointers issue I just cannot get past

Apprentice
Posts: 18
Joined: 2010.07
Post: #1
Hey everyone, I've been dealing with an EXC_BAD_ACCESS for the last few days that I just cannot seem to get past. Hopefully someone else can shed some light on my issue.

I have a class called Object, which right now contains only a vector<Vertex> for keeping track of the vertices of an object in the game world. Within my rendering class I have a vector<Object*>, as I would like to dynamically allocate objects at runtime depending on the map I generate. I build this vector<Object*> with the following loop

Code:
    for (int i = 0; i < 9; i++) {
        Object* objPntr = new Object;
        (*objPntr).BuildCone(coneX, coneY, .5, 1, 10);
        //coneX += 2;
        coneY += 1;
        if (coneY == 4) {
            coneX++;
            coneY = -4;
        }
        objVector.push_back(objPntr);
    }

Now, if I try and access this vector right after it it's initialization I have no problems. This happens within the same method of my rendering class, an init method. My problem arises when I get to the rendering method and have to draw everything. I do so with the following method

Code:
    for (int i = 0; i < objVector.size(); i++) {
        std::vector<Vertex> model = (*objVector[i]).ReturnModel();
        glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &model[0].Position.x);
        glColorPointer(4, GL_FLOAT, sizeof(Vertex), &model[0].Color.x);    
        glDrawArrays(GL_TRIANGLE_STRIP, 0, model.size());
    }

This results in a SIGABRT call from the debugger, with the following info from a backtrace (I've left out unnecessary calls)

Code:
#0  0x33b3db2c in __kill ()
#1  0x33b3db20 in kill ()
#2  0x33b3db14 in raise ()
#3  0x33b54e3a in abort ()
#4  0x33c5c398 in __gnu_cxx::__verbose_terminate_handler ()
#5  0x313918c0 in _objc_terminate ()
#6  0x33c59a8c in __cxxabiv1::__terminate ()
#7  0x33c59b04 in std::terminate ()
#8  0x33c59c2c in __cxa_throw ()
#9  0x33bfe5c4 in std::__throw_bad_alloc ()
#10 0x0000ae24 in __gnu_cxx::new_allocator<Vertex>::allocate (this=0x2fffe62c, __n=2415617216) at new_allocator.h:89
#11 0x0000816a in std::_Vector_base<Vertex, std::allocator<Vertex> >::_M_allocate (this=0x2fffe62c, __n=2415617216) at stl_vector.h:131
#12 0x0000b88a in std::_Vector_base<Vertex, std::allocator<Vertex> >::_Vector_base (this=0x2fffe62c, __n=2415617216, __a=@0x11cc00) at stl_vector.h:116
#13 0x0000ba12 in std::vector<Vertex, std::allocator<Vertex> >::vector (this=0x2fffe62c, __x=@0x11cc00) at stl_vector.h:232
#14 0x0000b31e in Object::ReturnModel (this=0x11cc00) at /Users/stephan/Code/OpenGL/Testbed1/Classes/Object.cpp:45
#15 0x00004a40 in RenderingEngine1::Render (this=0x11c710) at /Users/stephan/Code/OpenGL/Testbed1/Classes/oldtestbed.cpp:946

A lot of this code is extremely unefficient, but it is mostly for clarity until I find the root of my memory allocation issue. If I use vector<Object> instead of vector<Object*> (and make the necessary changes elsewhere) I have no problems, but I don't want that kind of limitation. A few different parts of my code are going to be using these objects and I want to be able to pass them around as pointers rather than constant copies being made. Understand, I've written this 10 different ways, including but not limited to just using a struct instead of a seperate class, my methods of constructing the vector<Object*> (performing a resize and iterating through rather than push_back), my draw methods (iterating rather than indexing). At one point it would work only if the number of elements in my vector was a multiple of 8.

Even more frustrating is the fact that through all the changes I've made every single one has performed exactly as expected on the simulator, which I imagine is due to the differences in memory management between the two environments. Also, I'm compiling using the 4.0 SDK for a 3.1.3 device and my iPhone is a 3G running 3.1.3.

Are my vector< ptr > ambitions just pipedreams? I am afraid it is a lack of understanding of the deeper quirks of STL that are causing me problems, but I don't feel like what I am doing is THAT far-fetched. Any and all insight is greatly appreciated.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #2
(Jul 16, 2010 06:58 AM)agreendev Wrote:  If I use vector<Object> instead of vector<Object*> (and make the necessary changes elsewhere) I have no problems, but I don't want that kind of limitation. A few different parts of my code are going to be using these objects and I want to be able to pass them around as pointers rather than constant copies being made.

You should try references for that:
http://www.cprogramming.com/tutorial/references.html
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2010.03
Post: #3
std::vector<Vertex> model = (*objVector[i]).ReturnModel()

this line looks suspicious to me. Firstly its more 'correct' to write

std::vector<Vertex> model = objVector[i]->ReturnModel()

but more importantly you are asking for a copy of the std::vector<Vertex> not a reference to it. What does ReturnModel return exactly?

std::vector<Vertex> &model = objVector[i]->ReturnModel()

look more likely to be what you want. Your idea seems sound. You want a vector of pointers, this should work. The std::exception you are getting is to do with allocating a new vector of Vertex

11 0x0000816a in std::_Vector_base<Vertex, std::allocator<Vertex> >::_M_allocate (this=0x2fffe62c, __n=2415617216)

I'd take a look at what ReturnModel is returning and why the vector copy constructor is failing and then see if you really DO want to copy the vertex data ReturnModel is supposed to be returning.

Hope that Helps.
Quote this message in a reply
Post Reply