2

在我的 freeglut 项目中,我分配了很多内存,当用户关闭 freeglut(或 glut)窗口时,我无法释放它,有什么想法吗?

4

2 回答 2

8

FreeGLUT 提供了几种解决方案:

  1. 你可以调用glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION)andglutLeaveMainLoop()glutMainLoop()函数返回,然后你可以在glutMainLoop()调用后释放你想要的所有内存。

  2. glutMainLoop()您可以像这样创建自己的事件循环,而不是调用:

    bool running = true;
    while (running)
    {
        glutMainLoopEvent();
    }And whenever you want to exit application - just set running variable to false, and free the allocated memory after while loop.
    
  3. 或者你什么也不做——任何现代操作系统都会在进程终止时正确地释放所有分配的内存。当然,如果您需要在终止时做一些特殊的事情——比如写入日志文件、发送网络数据包,那么您必须手动执行此操作。

于 2012-06-20T06:52:53.673 回答
2

Register an exit function with atexit(onexit) before the main loop.

于 2013-11-10T23:00:59.073 回答