How to recover memory after glutMainLoop activation?

According to OpenGL documentation,
3.1 glutMainLoop

glutMainLoop enters the GLUT event loop.

Using

void glutMainLoop(void);

Description glutMainLoop is part of the GLUT event loop. This procedure must be called no more than once in the GLUT program. After the call, this procedure will never return. It will call as necessary any callbacks that have been registered.

Therefore, whenever glutMainLoop () is called, it will never return. As a result, I could not free my memory after allocation. My problem: I need to load an image from a file, the solution of the book (Superbible 4th edition) is to place this boot file procedure inside the drawing function. However, I realized that this method is too expensive due to several opening and closing files. I recalled from my class Data structure when studying the B-tree, the cost of accessing external resources is significant, so I try to avoid as much as I can. So my alternative solution is to put this loading image procedure inside an installed scene function that is called only once. But now I am facing another problem, I cannot delete the memory due toglutMainLoop. What can I do in this situation? I am new to openGL, so I really don't know how to deal with this particular problem. Any idea would be greatly appreciated.

#include <cstdio> 
#include <cstdlib>
#include <iostream>

#include "Utility.h"
#include "TgaHeader.h"
#include "TgaImage.h"

#include <GL/glut.h>

using namespace std;

TgaImage* image = NULL;

void setupScene() {
    // set color background
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    // load image from file
    image = loadTgAFile( "Fire.tga" );
}

void renderScene() {
    // clear color
    glClear( GL_COLOR_BUFFER_BIT );
    // TGA format is 1 byte aligned
    glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
    glRasterPos2i( 0, 0 );
    if( image != NULL ) {
        glDrawPixels(  
            image->header.width,  
            image->header.height,
            image->format,
            GL_UNSIGNED_BYTE,
            image->pixels
        );
    }
    glutSwapBuffers();
}

void resizeWindow( int w, int h ) {
    if( h == 0 ) {
        h = 1;
    }
    glViewport( 0, 0, w, h );
    // reset coordinate before modifying
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    // set the clipping volume
    gluOrtho2D( 0.0f, w, 0.0f, h );
    // reset to modelview matrix
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}

int main( int argc, char** argv ) {
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
    glutInitWindowSize( 512, 512 );
    glutCreateWindow( "Image" );
    // register callback
    glutReshapeFunc( resizeWindow );
    glutDisplayFunc( renderScene );
    // initialize scene
    setupScene();
    glutMainLoop();
    // it will never reach this 
    delete image;
}

Thank,

+3
source share
2 answers

The “recommended” mechanism is to use a function atexitor onexitto schedule a function to be called upon exiting a program:

void exitProgram() {
    // cleanup code
}

// then put this in main():
atexit( exitProgram );

Please note that as soon as the program exits, the operating system clears all resources, including the memory that the program used, so it does not have a technical memory leak.

+6
source

, . . exit, .


, , , glDrawPixels. ( ), . , , , .

+2

All Articles