I'm trying to add some objects to a vector of the same type. In the header:
std::vector<Object_3D> balls_;
I'm trying to push objects onto the back of the vector with this code:
void S3DApp::InitGameObjects(){
int i;
Object_3D ball_;
for(i = 0; i < ball_count_/2; i++){
ball_.Init(platform_, "stripe.Obj");
ball_.SetScale(abfw::Vector3(0.2, 0.2, 0.2));
ball_.SetTranslation(abfw::Vector3((float)i, 1.0f, (float)i));
balls_.push_back(ball_);
}
}
The init function loads a model from a object file:
void Object_3D::Init(abfw::Platform& platform_, const char *filename){
abfw::OBJLoader obj_loader;
obj_loader.Load(filename, platform_, model_);
mesh_instance_.set_mesh(model_.mesh());
transform_.SetIdentity();
mesh_instance_.set_transform(transform_);
position_ = transform_.GetTranslation();
}
I get an error on the third run through the for loop because the object_3d destructor is called. But I'm not sure why. The destructor is where the call to Model::Release() is made I think which is on top of the call stack. This is the call stack:
s3d_app.exe!abfw::Model::Release() Line 26 C++
s3d_app.exe!Object_3D::~Object_3D() Line 9 C++
s3d_app.exe!Object_3D::`scalar deleting destructor'(unsigned int) C++
s3d_app.exe!std::allocator<Object_3D>::destroy<Object_3D>(Object_3D * _Ptr) Line 624 C++
s3d_app.exe!std::allocator_traits<std::allocator<Object_3D> >::destroy<Object_3D>(std::allocator<Object_3D> & _Al, Object_3D * _Ptr) Line 758 C++
s3d_app.exe!std::_Wrap_alloc<std::allocator<Object_3D> >::destroy<Object_3D>(Object_3D * _Ptr) Line 909 C++
s3d_app.exe!std::_Destroy_range<std::_Wrap_alloc<std::allocator<Object_3D> > >(Object_3D * _First, Object_3D * _Last, std::_Wrap_alloc<std::allocator<Object_3D> > & _Al, std::_Nonscalar_ptr_iterator_tag __formal) Line 89 C++
s3d_app.exe!std::_Destroy_range<std::_Wrap_alloc<std::allocator<Object_3D> > >(Object_3D * _First, Object_3D * _Last, std::_Wrap_alloc<std::allocator<Object_3D> > & _Al) Line 80 C++
s3d_app.exe!std::vector<Object_3D,std::allocator<Object_3D> >::_Destroy(Object_3D * _First, Object_3D * _Last) Line 1480 C++
s3d_app.exe!std::vector<Object_3D,std::allocator<Object_3D> >::_Reallocate(unsigned int _Count) Line 1515 C++
s3d_app.exe!std::vector<Object_3D,std::allocator<Object_3D> >::_Reserve(unsigned int _Count) Line 1532 C++
s3d_app.exe!std::vector<Object_3D,std::allocator<Object_3D> >::push_back(const Object_3D & _Val) Line 1199 C++
I have tried creating the ball object as a pointer and adding it to an array of pointers. I have tried creating the ball as an object and adding it to the array as a pointer. I have tried creating the ball object within the for loop.
Edit: Object_3D code
#include <graphics/mesh_instance.h>
#include <graphics/model.h>
#include <assets/png_loader.h>
#include <assets/obj_loader.h>
#include <maths/vector3.h>
class Object_3D{
public:
Object_3D();
~Object_3D();
void Init(abfw::Platform& platform, const char*);
void SetTranslation(abfw::Vector3 transform_);
void SetScale(abfw::Vector3 scale_);
void Move(abfw::Vector3 move_);
abfw::Model& GetModel();
abfw::MeshInstance GetMeshInstance();
abfw::Matrix44 GetTransform();
abfw::Vector3 position_;
private:
abfw::Model model_;
abfw::MeshInstance mesh_instance_;
abfw::Matrix44 transform_;
};
cpp.
#include "Object_3D.h"
Object_3D::Object_3D(){
}
Object_3D::~Object_3D(){
model_.Release();
}
void Object_3D::Init(abfw::Platform& platform_, const char *filename){
abfw::OBJLoader obj_loader;
obj_loader.Load(filename, platform_, model_);
mesh_instance_.set_mesh(model_.mesh());
transform_.SetIdentity();
mesh_instance_.set_transform(transform_);
position_ = transform_.GetTranslation();
}
abfw::Model& Object_3D::GetModel(){
return model_;
}
abfw::MeshInstance Object_3D::GetMeshInstance(){
return mesh_instance_;
}
void Object_3D::SetTranslation(abfw::Vector3 position_vector_){
position_ = position_vector_;
transform_.SetTranslation(position_);
mesh_instance_.set_transform(transform_);
}
void Object_3D::Move(abfw::Vector3 move_){
position_ += move_;
transform_.SetTranslation(position_);
mesh_instance_.set_transform(transform_);
}
void Object_3D::SetScale(abfw::Vector3 scalingVector){
transform_.Scale(scalingVector);
mesh_instance_.set_transform(transform_);
}
abfw::Matrix44 Object_3D::GetTransform(){
return transform_;
}
InitGameObjects, you're creating a singleObject_3D, then adding it to the vector, than changing that same object, and adding it (again), and again, and again. You need to create a new one every iteration (probably withstd::make_shared, then add the resultingshared_ptr<Object_3D>to the vector instead.Object_3Dfully Rule of Three compliant?