PDA

View Full Version : How to delete at the end of an OpenGL program ???



VinceDeNice
05-30-2005, 02:38 AM
Hi,
I've got a simple opengl program, in a file my_app.cpp
I declare in the global scope a pointer:


my_object* obj = new my_object();I'd like to know WHEN I can delete this object ???
I tried it:


int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
glutInitWindowPosition(0, 0);
glutCreateWindow("demo_route");

initGL();
initialise_scene();


glutDisplayFunc(&callback_display);
glutReshapeFunc(&callback_reshape);
glutKeyboardFunc(&callback_keyboard);
glutSpecialFunc(&callback_special);
glutMouseFunc(&callback_mouse);
glutMotionFunc(&callback_motion);
glutTimerFunc(elapsedUSecs, timerCallback, 0);

glutMainLoop();

delete obj; // I NEVER REACH THIS LINE !!!

return 0;
}I think it's very easy, so anyone has got the anwer ?

thanks a lot.

Ehsan Kamrani
05-30-2005, 02:45 AM
You can use from the mouse or keyboard functions to delete the object. As an example:
void mouse( int btn, int state, int x, int y )
{
if( btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
{
ExitFromPogram();
}

And your ExitFromProgram can be in this form:
void ExitFromProgram()
{
if( object )
free object;
exit(0);
}

In your previous code, you never can free the object, since it has been called after your glutMainLoop().If you free the object before the glutMainLoop(), you never can use from that object in your program, since it is deleted in the first loop.none-glut functions in your main function are executed only in the first loop.

-Ehsan-

RigidBody
05-30-2005, 02:53 AM
the glut doc advises to use

int on_exit(void (*function)(int , void *), void *arg);

from <stdlib.h> to register a user function which will be called when the app exits.

example:


#include<stdio.h>
#include<stdlib.h>

class foo {
public: foo() { printf("\tcreating foo\n"); };
~foo() { printf("\tdeleting foo\n"); }; };

foo *new_foo;

void my_exit(int i, void *ptr) {

delete(new_foo); }

int main(int argc, char *argv[]) {

new_foo = new foo;
on_exit(my_exit, NULL); }
//
// gcc -o fooclass fooclass.cc
//

jide
05-30-2005, 03:32 AM
prefer use at_exit instead of on_exit for c++ programs.

RigidBody
05-30-2005, 03:44 AM
ok. but why?

VinceDeNice
05-30-2005, 04:49 AM
at_exit doesn't work, but on_exit does.
thanks.

jide
05-30-2005, 08:56 AM
RigidBody, simply because atexit has been redefined in the C++ standard library, so might not provokes some mistakes due to a C-only function call. As you know C and C++ are no longer 100% compatible. This is almost true for the standard library.

VinceDeNice, I don't know why at_exit doesn't work, maybe because it is called atexit and not at_exit :) . It's defined in cstdlib if I'm not wrong.

Ehsan Kamrani
05-30-2005, 09:55 AM
I just searched in MSDN. Yes it is atexit and is defined at <stdlib.h> :)
Libraries:
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
-Ehsan-