Why does my c++ object gets deleted as soon as i create it? -


i have created class make possible render multiple objects on screen, called "entity", here code 3 constructors:

entity::entity() {     x = y = z = 0;     std::cout<<"init model"<<std::endl;     model = model();     model.loadmodel2("huis_0.txt"); }  entity::entity(float xval,float yval,float zval,std::string source) {     std::cout<<"init model2"<<std::endl;     x = xval;     y = yval;     z = zval;     model = model();     model.loadmodel2(source); } entity::entity(std::string source) {     x = y = z = 0;     std::cout<<"init model3"<<std::endl;     model = model();     model.loadmodel2(source); } 

the deconstructor simple prints out "deleting entity". load entity in main file using second method. model class loads vertices , indices file. during run output get:

init2 ++loading model+++ ++done loading model++ deleting entity //other prints deleting entity 

then after close window. following error: "debug failed: expression:_block_type_is_valid(phead->nblockuse) " guess error because delete object twice. how prevent this? entity have can't it.

edit: entity object gets created outside method, @ top of main file because wanted use entity's "render()" method.

edit2: functions sword used, please excuse me being messy, trying out opengl codes learned:

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,&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,&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,&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,&transform_persp[0][0]);     gluseprogram(0);      glgenbuffers(1,&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<834;i++) model_test[i] = model.getindex(i);     glbufferdata(gl_array_buffer,sizeof(model_test),&model_test,gl_static_draw);      glvertexattribpointer(0,3,gl_float,gl_false,0,buffer_offset(0));     glenablevertexattribarray(0);      gluint ibo;     glgenbuffers(1,&ibo);     glbindbuffer(gl_element_array_buffer,ibo);     glshort indices[1656];//1656     memset(indices,0,sizeof(indices));     for(int i=0;i<1656;i++) indices[i] = model.getvertexindex(i);     glbufferdata(gl_element_array_buffer,sizeof(indices),&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,&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,&sword.gettransform()[0][0]);     sword.render();      getglerrors("draw");      glutswapbuffers();     glutpostredisplay(); } 

i'm sure how you're creating entity, creating temporary model, copying model, , destroying temporary:

model = model(); 

i bet you're doing similar entity.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -