What is the nicest way to close FreeGLUT?
Asked Answered
C

4

13

I'm really having trouble closing my console application with FreeGLUT.

I would like to know what the best way is to take every possible closing, because I don't want any memory leaks (I'm pretty afraid of those).

So I already tried the following, which is giving me an exception like this:

First-chance exception at 0x754e6a6f in myProject.exe: 0x40010005: Control-C.

int main(int argc, char **argv)
{
    if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, true) )
    {
        // more code here as well ....


        glutCloseFunc(close); // set the window closing function of opengl
        glutMainLoop();
        close(); // close function if coming here somehow
    }
    else
    {
        return 1;
    }
    return 0;
}

void close()
{
    // keyboardManager is a pointer to a class
    // which I want to delete, so no memory will leak.
    if(keyboardManager) // do I need this check?
        delete keyboardManager;
}

bool CtrlHandler(DWORD fdwCtrlType)
{
    switch(fdwCtrlType)
    {
        // Handle the CTRL-C signal.
        case CTRL_C_EVENT:
        // and the close button
        case CTRL_CLOSE_EVENT:
          close();
          return true;

        // Pass other signals to the next handler. 
        case CTRL_BREAK_EVENT:
            return false;

    // delete the pointer anyway
        case CTRL_LOGOFF_EVENT:
        case CTRL_SHUTDOWN_EVENT:
        default:
            close();
            return false; 
    } 
}

So what goes right is:

  1. Closing the window of glut
  2. Closing the console application with the x
  3. Closing my window of glut with my keyboardmanager if(keyboardManager->isKeyDown[27]) glutExit();

What goes wrong is:

  1. Closing the console application with CTRL+C, it gives the exception from above.

This is in Visual Studio 2008 C++.

UPDATE

I found that the exception is thrown, because I'm in debug. So that won't be a problem. But the question is still open: What is the most elegant way to actually close glut?

atexit() seems to work as well, so maybe I can use this?

Chelsea answered 17/2, 2011 at 19:51 Comment(5)
You don't need to worry about freeing memory when your program shuts down, the OS reclaims everything and nothing leaks. Persistent resources like files should still be cleaned up properly of course.Tempura
atexit() probably won't solve your problem of C++ objects cleanup.Galligaskins
@Ben, @wilhelmtell: But I should clear my keyboardManager object for example, because my Progam holds a pointer to it, right?Chelsea
If it uses some resources that survive the termination of your program, yes. But the OS will reclaim memory and should also revoke keyboard input capture, so a keyboard manager probably doesn't need cleanup. There's even a school of thought that says not to clean up persistent resources either, the rationale being that eventually your cleanup won't get a chance to run (catastrophic power failure perhaps), so you need logic to recover dirty state. And if you can deal with dirty state, then any cleanup is just a waste of time.Tempura
If you care about opengl leaks (say you're recreating things or you startup and tear down OpenGL more than once) then you can use GDebugger to report all leaked OpenGL objects. It also reports all sorts of useful things that you may be doing incorrectly, like redundant state changes, or using deprecated functionality.Elohim
H
17

I use this function:

void glutLeaveMainLoop ( void ); 

There is more information on their sourceforge page but I never used that functionality:

The glutLeaveMainLoop function causes freeglut to stop the event loop. If the GLUT_ACTION_ON_WINDOW_CLOSE option has been set to GLUT_ACTION_CONTINUE_EXECUTION, control will return to the function which called glutMainLoop; otherwise the application will exit.

http://freeglut.sourceforge.net/docs/api.php#EventProcessing

It is safe to use delete on a null pointer, no need to check.

Heinrich answered 21/2, 2011 at 0:51 Comment(6)
Seems indeed interesting. So I could call this function in the SetConsoleCtrlHandler and in the keychecker to check ESC. So I could then put my pointer deleters in the main function probably?Chelsea
It is far better to setup glutCloseFunc to call a function that cleans up. FreeGLUT will call it before it tries to tear down the context, unlike doing it after the mainloop.Elohim
It really isn't a big deal to clean up properly, but if you're using a tool like GDebugger that complains if you don't clean up, you can use glutCloseFunc to get clean runs.Elohim
Okay so according to the latest GLUT specification (3.7), glutLeaveMainLoop() does not exist anymore. So now what would be the answer to the OP's question?Ina
glutLeaveMainLoop still exists within FreeGLUT (and I believe OpenGLUT, though unverified, but their subwin sample uses it). Tested with latest stable version, 3.0.0 - it's an extension to the regular GLUT.Epitome
Ubuntu has it but there's a trick. GLUT on Ubuntu is all FreeGLUT, and the FreeGLUT is split into _std and _ext header files. If you include GL/glut.h it just includes freeglut_std.h. You have to include GL/freeglut.h instead, and that includes both the _std and freeglut_ext.h headers, and then you get glutLeaveMainLoop().Ainslie
I
5

Thanks to Maarten's post, this works to me:

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);

whenever you want to leave the mainloop without termiante the application use:

glutLeaveMainLoop();

don't forget to include "freeglut.h"

Irony answered 29/9, 2016 at 14:18 Comment(0)
B
1

I use glutDestroyWindow(int handle);

or

According to ID: RigidBody at OpenGL forum

void destroy_window() 
{
 window_valid = -1;
}

void display_func() 
{
 if(window_valid == -1)
   return;
 // draw things
}
Burra answered 6/7, 2016 at 0:6 Comment(0)
C
0

Try this method:

glutDestroyWindow(glutGetWindow());
Caveat answered 1/10, 2018 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.