Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5993789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:43:32+00:00 2026-05-22T23:43:32+00:00

According to OpenGL documentation, 3.1 glutMainLoop glutMainLoop enters the GLUT event processing loop. Usage

  • 0

According to OpenGL documentation,
3.1 glutMainLoop

glutMainLoop 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();
    // it will never reach this 
    delete image;
}

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:43:33+00:00Added an answer on May 22, 2026 at 11:43 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It appears that glColorMaterial() is absent from OpenGL ES . According to this post
According to this discussion , the iphone agreement says that it doesn't allow loading
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to
According to the answers to this question, I cannot embed a file version in
According to this article Silverlight 2 Beta 2 supports the DataContractJsonSerializer object. But, when
According to the feedparser documentation , I can turn an RSS feed into a
I'm a little confused as to the proper usage of VBOs in an OpenGL
According to Five Simple Database Design Errors You Should Avoid by Anith Sen, using
I have a glutSolidTeapot (which has its surface normals generated automatically according to opengl.org)
According to the REST philosophy, a PUT request should update the resource at a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.