Question

I have a class with a static variable. Since I need a constructor that isn't the default, I'm getting a little confused, but I hope I did it well

Class

class Object3D{

    public:
        static Object3D ObjControl;

        Object3D(); //this is here just for the initialization of the static variable

        Object3D(Triangle *mesh);

        Triangle *mesh;

};

At this point I need to create an Object3D and I do as below

bool Engine::OnInit() {

    if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
            return false;
        }

        if((Surf_Display = SDL_SetVideoMode(WIDTH, HEIGTH, BBP, FLAGS)) == NULL) {
            return false;
        }

        arma::colvec::fixed<3> upDirection;
        upDirection << 0 << 1 << 0;

        Camera cam(0.0, 0.0, 10.0, 10.0, 200.0, 90.0, upDirection);
        Camera::CameraControl = cam;

        arma::colvec::fixed<3> vertexA;
        vertexA << -1 << 1 << 0;

        arma::colvec::fixed<3> vertexB;
        vertexB << 1 << 1 << 0;

        arma::colvec::fixed<3> vertexC;
        vertexC << 0 << -1 << 0;

        Triangle tri(vertexA, vertexB, vertexC);
        Triangle mesh[1];
        mesh[0] = tri;
        Object3D obj(mesh);

        Object3D::ObjControl = obj; // PROBLEM! -> when the function extis from the OnInit ObjControl doesn't have anything inside.. it is like cleaned at the exit


        return true;
    }

The problem is the one that is inserted in the comment before the return.

Then when I need to pass that object to the rendering function, as below; the application closes because I'm trying to access to a location of memory not initialized

void Engine::OnRender(){

    Rendering.WfRender(Object3D::ObjControl, Surf_Display, 1);

}

I think I'm doing something wrong with the static variable, but I did the same with a static variable for a Camera class, as you can see in the Engine::OnInit, and there everything works well. So I have no clue what's going on.

Was it helpful?

Solution 2

  • Does your Object3D class only hold onto the pointer to the mesh or take a copy of it?
  • Does it implement a deep-copy copy constructor?

I ask because your mesh is going out of scope after being assigned to obj, and obj is going out of scope after being assigned to the static variable. You need to either assign the mesh on the heap and hand that pointer to the static variable, or ensure the actual data is copied by correctly implementing the right constructors.

EDIT: Or, as this looks like games development, get it done quick and nasty! ;-)

Object3D::ObjControl.mesh = new Triangle(vertexA, vertexB, vertexC);

...and lose the local variables tri, mesh, and obj.

OTHER TIPS

The main issue in your program is that you make a Triangle instance (mesh) in your function and that you pass a pointer to your static member variable ObjControl. When you leave the function, mesh is no longer available, so ObjControl points to an invalid instance. This could be solved by storing an actual Triangle instead of a pointer to a Triangle in Object3D or a container of Triangles if more are needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top