如何在glutMainLoop被激活后回收内存
How to reclaim memory after glutMainLoop is activated?
根据OpenGL文档,
3.1 glutMainLoop
glutMainLoop进入GLUT事件处理循环。
用法
void glutMainLoop(void);
描述glutMainLoop进入GLUT事件处理循环。在GLUT程序中,此例程最多应调用一次。一旦调用,此例程将永远不会返回。它将根据需要调用任何已注册的回调。
因此,无论何时调用glutMainLoop(),它都不会返回。因此,分配后我无法释放内存。我的问题是:我需要从文件中加载图像,这本书(Superbible第4版)的解决方案是将这个加载文件的例程放入绘图功能中。然而,由于打开和关闭多个文件,我意识到这种方法过于昂贵。我回忆说,在学习B树时,在我的数据结构课上,访问外部资源的成本是相当大的,所以我尽量避免。因此,我的另一个解决方案是将这个加载图像例程放入只调用一次的设置场景函数中。但我现在面临另一个问题,因为glutMainLoop
,我无法删除内存。在这种情况下我该怎么办?我是openGL的新手,所以我真的不知道如何处理这个特殊的问题。任何想法都将不胜感激。
#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;
}
谢谢,
"推荐"机制是使用atexit
或onexit
函数来调度程序退出时要调用的函数:
void exitProgram() {
// cleanup code
}
// then put this in main():
atexit( exitProgram );
请注意,一旦程序退出,操作系统就会清理程序正在使用的所有资源,包括内存,因此从技术上讲,它不会泄漏内存。
每个正常的操作系统都会在进程退出后回收其占用的内存。因此,您不需要做任何事情。当您调用exit
时,内存将被释放。
程序速度慢的另一个原因是您使用的是glDrawPixels
。一种合适的方法是在纹理中加载图像(在进入主循环之前进行),并在显示回调中渲染纹理。这样,你很可能会看到你的程序有很大的改进。
相关文章:
- 将字符串存储在c++中的稳定内存中
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- Win32编译器选项和内存分配
- 当vector是tje全局变量时,c++中vector的内存管理
- 带内存和隔离功能的SQLite
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 迭代时从向量和内存中删除对象
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 为什么示例代码访问IUnknown中已删除的内存
- 如何在C++类内存结构中创建"spacer"?
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 内存模型:激活记录
- 可用于激活堆栈的内存
- 如何在glutMainLoop被激活后回收内存