I have the following issue:
In a C++ program I have a global data structure declared as Renderer Rendering_Handler
, which contains a member field defined as vector<Render_Info> visble objects
.
What the data structures themselves are doing is not important, they are wrappers needed to abstract data to parallelize my program.
To be clear, Rendering_Handler is completely global, and it's in fact a singleton (I can 100% confirm that the constructor has been called once and only once for this class).
I have declared the following class method:
Render_Info* Renderer::add_Render_Info()
{
visible_objects.push_back(Render_Info());
return &(visible_objects.back());
}
Simple enough, it creates a new Render_Info
structure, appends it to the visible_objects
array and returns a pointer to the object.
A different data structure called Chunk
has a constructor defined as
Chunk::Chunk(vec3 offset, World* w)
{
/*initialize some values*/
draw_info = Rendering_Handler->add_Render_Info();
draw_info->VBOs = vector<GLuint>(5);
/*initialize OpenGL VAOs, VBOs and other buffer objects*/
cout << draw_info->VBOs.size() << endl;
cout << draw_info << endl;
}
It also has a method defined as:
void Chunk::update_render_info()
{
cout << draw_info->VBOs.size() << endl;
cout << draw_info << endl;
/*OpenGL stuff*/
}
And finally
We have the method that initializes everything:
World::World()
{
/*Initialize chunks in a circular 3D array*/
loaded_chunks = new Chunk_Holder(h_radius, h_radius, v_radius, this);
for(int i=0; i<h_radius; i++)
{
for(int j=0; j<h_radius; j++)
{
for(int k=0; k<v_radius; k++)
{
(*loaded_chunks)(i,j,k)->update();
}
}
}
}
The output of the rpgram is:
...
Let's focus on the frist 2 and last 2 lines of the output, which correspond to the print statements I have added for debugging.
The first 2 lines indicate that 5 elements have been added to the buffer at location 0x556edb7ae200
The last 2 lines tell me that the same buffer (same since the memory location is the same) now contains 0 elements.
As you can see from the snaps of the code, no function is called in between creating the Chunks and updating them. Does anybody have an idea of what could be causing the dissapearance of these elements?
Have I not correctly reserved the memory? Are these objects being cleared without my knowledge due to wrong allocation?
I think the problem is that you're storing pointers to elements in vector and at the same time you keep calling vector::push_back
which every now and then has to resize the vector and move all elements to new memory chunk. This will invalidate all the pointers you have obtained before.
To give you more context: vector
stores its elements in a continuous chunk of memory. When vector::push_back
is called and there's no free space left in this chunk of memory then vector
will allocate another chunk of memory with twice the size of the old chunk. Then it will copy/move all the elements from old chunk to the new one. Finally the old chunk will be destroyed. When you call &(visible_objects.back())
you will get an address in memory that's inside current memory chunk owned by visible_objects
. If later visible_objects.push_back
is called and visible_objects
has to migrate to new bigger memory chunk then all the addresses obtained before will be stale as they point to the old chunk of memory that has been destroyed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With