How to reclaim memory after glutMainLoop is activated?
According to OpenGL documentation,
3.1 glutMainLoopglutMainLoop enters the GLUT event processing loop.
Usage
void glutMainLoop(void);
Description glutMainLoop enters the GLUT event processing loop. This routine should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered.
So whenever glutMainLoop() is called, it will never return. As consequence, I could not release my memory after allocating.
My problem is:
I need to load an image from file, the book (Superbible 4th edition) solution is to put this loading file routine inside the drawing function. However, I realized this method was too expensive due to multiple opening and closing files. I recalled from my Data structure class when studying B-tree, the cost of accessing external resources is considerable, so I try to avoid as much as I can.
So my alternative solution is to put this loading image routine inside the set up scene function which is called only once. But then I now face another issue, there is no way I delete memory because of the glutMainLoop
.
What can I do in this situation? I'm new to openGL so I really don't know to how to handle 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();
开发者_JS百科 // it will never reach this
delete image;
}
Thanks,
The "recommended" mechanism is to use the atexit
or onexit
function to schedule a function to be called when the program is exiting:
void exitProgram() {
// cleanup code
}
// then put this in main():
atexit( exitProgram );
Note that as soon as a program exits, the operating system cleans up all resources, including memory, that the program was using, so it's not technically leaking memory.
Every normal operating system is going to reclaim a memory occupied by a process after it exit. Therefore, you do not need to do anything. When you call exit
, the memory will be released.
Another reason why your program is slow is because you are using glDrawPixels
. A proper way would be to load the image in a texture (you do it before entering the main loop), and in the display callback render the texture. This way, you would most likely see great improvement of your program.
精彩评论