title: Why does my c++ object gets deleted as soon as i create it?
body: <p>I have created a class to make it possible to render multiple objects on the screen, which i called "Entity", here is the code for the 3 constructors:</p>
  
  <pre><code>Entity::Entity()
  {
      x = y = z = 0;
      std::cout&lt;&lt;"INIT MODEL"&lt;&lt;std::endl;
      model = Model();
      model.loadModel2("huis_0.txt");
  } 
  Entity::Entity(float xVal,float yVal,float zVal,std::string source)
  {
      std::cout&lt;&lt;"INIT MODEL2"&lt;&lt;std::endl;
      x = xVal;
      y = yVal;
      z = zVal;
      model = Model();
      model.loadModel2(source);
  }
  Entity::Entity(std::string source)
  {
      x = y = z = 0;
      std::cout&lt;&lt;"INIT MODEL3"&lt;&lt;std::endl;
      model = Model();
      model.loadModel2(source);
  }
  </code></pre>
  
  <p>The deconstructor simple prints out "deleting entity".
  Then i load an entity in my main file using the second method.
  Model is another class that simply loads the vertices and indices from a file.
  During the run this is the output i get:</p>
  
  <pre><code>INIT2
  ++loading model+++
  ++done loading model++
  deleting entity
  //other prints
  deleting entity
  </code></pre>
  
  <p>then after i close my window. i get the following error: "debug failed: expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse) "
  My guess is i get this error because i delete the object twice. How do i prevent this?
  That is also the only entity i have so that can't be it.</p>
  
  <p>EDIT: The Entity object gets created outside any method, at the top of my main file because i wanted to use the Entity's "render()" method.</p>
  
  <p>EDIT2: This are all the functions where sword is used, please excuse me for it being messy, i was trying out the openGL codes i learned:</p>
  
  <pre><code>Entity sword = Entity(0,0,0,"pirate_sword.txt");
  
  void Initialize(int argc, char* argv[])
  {
      InitWindow(argc, argv);
  
      fprintf(stdout,"INFO: OpenGL Version: %s\n",glGetString(GL_VERSION));
      glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  
      //model.loadModel2("huis_0.txt");
      model.loadModel2("test_zwaard_0.txt");
  
      glGenVertexArrays(1,&amp;vao);
      glBindVertexArray(vao);
  
      ShaderLoader loader;
      std::string test = loader.loadStringFromFile("vertex_shader.txt");
      std::string frag = loader.loadStringFromFile("fragment_shader.txt");
      program = loader.loadProgram(GL_VERTEX_SHADER,test,GL_FRAGMENT_SHADER,frag);
      glUseProgram(program);
  
      /*LIGHTING*/
      ambient_loc = glGetUniformLocation(program,"ambient");
      lightColor_loc = glGetUniformLocation(program,"lightColor");
      lightDirection_loc = glGetUniformLocation(program,"lightDirection");
      halfVector_loc = glGetUniformLocation(program,"halfVector");
      shiny_loc = glGetUniformLocation(program,"shiny");
      strength_loc = glGetUniformLocation(program,"strength");
      GLfloat ambient[3] = {
          0.4,0.4,0.4
      };
      glUniform3f(ambient_loc,ambient[0],ambient[1],ambient[2]);
      GLfloat lightColor[3] = {
          0.4,0.4,0.4
      };
      glUniform3f(lightColor_loc,lightColor[0],lightColor[1],lightColor[2]);
      GLfloat lightDirection[3] = {
          1.0,0.2,0.4
      };
      glUniform3f(lightDirection_loc,lightDirection[0],lightDirection[1],lightDirection[2]);
      GLfloat halfVector[3] = {
          1.0,0.2,0.4
      };
      glUniform3f(halfVector_loc,halfVector[0],halfVector[1],halfVector[2]);
      float shiny = 0.2f;
      glUniform1f(shiny_loc,shiny);
      float strength = 0.7f;
      glUniform1f(strength_loc,strength);
  
      /*END OF LIGHTING*/
      /*TRANSFORM*/
      persp_loc = glGetUniformLocation(program,"persp");
      model_loc = glGetUniformLocation(program,"model");
      camera_loc = glGetUniformLocation(program,"camera");
  
      glm::mat4 transform_model = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f));
          glm::rotate(transform_model,20.0f,glm::vec3(x,y,z));
          glUniformMatrix4fv(model_loc,16,false,&amp;transform_model[0][0]);
      glm::mat4 transform_camera = (glm::lookAt(glm::vec3(3,5,-5),glm::vec3(0,0,0),glm::vec3(0,1,0)));
          glUniformMatrix4fv(camera_loc,16,false,&amp;transform_camera[0][0]);
      glm::mat4 transform_persp = glm::perspective(45.0f,float(4/3),(float)0.5,(float)100);
  
      //Matrix4 m;
      //m.translateMatrix(0.2f,0.0f,0.0f);
      float frustumScale = 1.0f,zNear = 0.1f,zFar = 100.0f;
      GLfloat persp[16];
      memset(persp,0.0f,sizeof(persp));
      persp[0] = frustumScale;
      persp[5] = frustumScale;
      persp[10] = (zFar + zNear) / (zNear - zFar);
      persp[14] = (2 * zFar * zNear) / (zNear - zFar);
      persp[11] = -1.0f;
      glUniformMatrix4fv(persp_loc,16,false,&amp;transform_persp[0][0]);
      glUseProgram(0);
  
      glGenBuffers(1,&amp;vbo);
      glBindBuffer(GL_ARRAY_BUFFER,vbo);
      GLfloat model_test[834]; //834
      memset(model_test,0.0f,sizeof(model_test));
      for(unsigned int i=0;i&lt;834;i++) model_test[i] = model.getIndex(i);
      glBufferData(GL_ARRAY_BUFFER,sizeof(model_test),&amp;model_test,GL_STATIC_DRAW);
  
      glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(0));
      glEnableVertexAttribArray(0);
  
      GLuint ibo;
      glGenBuffers(1,&amp;ibo);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ibo);
      GLshort indices[1656];//1656
      memset(indices,0,sizeof(indices));
      for(int i=0;i&lt;1656;i++) indices[i] = model.getVertexIndex(i);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),&amp;indices,GL_STATIC_DRAW);
  
      getGLerrors("init");
  
      glEnable(GL_DEPTH_TEST);
  
      getGLerrors("depth");
  
      sword.init();
      sword.move(0.3f,0.0f,0.0f);
  
      getGLerrors("sword init");
  }
  void RenderFunction(void)
  {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
      glUseProgram(program);
          glm::mat4 transform_model = glm::mat4(0.5f);
          glm::rotate(transform_model,20.0f,glm::vec3(x,y,z));
          glUniformMatrix4fv(model_loc,16,false,&amp;transform_model[0][0]);
      //glDrawArrays(GL_TRIANGLES,0,model.getSize()/3);
      glBindVertexArray(vao);
      if(rasterize)glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
      else glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
      glDrawElements(GL_TRIANGLES,model.getIndexSize(),GL_UNSIGNED_SHORT,0);
  
      glUniformMatrix4fv(model_loc,16,false,&amp;sword.getTransform()[0][0]);
      sword.render();
  
      getGLerrors("draw");
  
      glutSwapBuffers();
      glutPostRedisplay();
  }
  </code></pre>