GLUT // Memory problem

Started by
6 comments, last by cbastien 16 years, 9 months ago
hello ! When i create a window using GlutCreateWindow() and then delete it using glutDestroyWindow, there are 10Mb of memory i can't get back, do you know why ? thx everybody ! here i made an "as most simple as possible" program to try to resolve this problem.

#include <gl\glut.h>
		
//functions list
int main(int argc, char **argv);				       
GLvoid window_display(); 
GLvoid window_key(unsigned char key, int x, int y); 

//window
#define WIDTH 800
#define HEIGHT 800

int WindowName;

//-----------------------------------------------------------------------------
// Name: Main
// Desc: Main window
//-----------------------------------------------------------------------------
int main(int argc, char **argv) 
{  
  glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

  // définition et création de la fenêtre graphique
  glutInitWindowSize(WIDTH, HEIGHT);
  WindowName=glutCreateWindow("Memory test");

  glutDisplayFunc(&window_display);
  glutKeyboardFunc(&window_key);

  glutMainLoop();  

  return 1;
}

//-----------------------------------------------------------------------------
// Name: window_display()
// Desc: 
//-----------------------------------------------------------------------------
GLvoid window_display()
{
}
	
//-----------------------------------------------------------------------------
// Name: window_key(unsigned char key, int x, int y) 
// Desc: keyboard
//-----------------------------------------------------------------------------
GLvoid window_key(unsigned char key, int x, int y) 
{  
  switch (key) {    
  case 27:  
    exit(1);                
    break; 
  case 'd':  
	glutDestroyWindow(WindowName);
	break; 
  }     
}



Advertisement
How do you know you have 10 mb of memory not being freed?
That memory is usually freed after your program terminates.
If you're using the task manager to watch your app's memory usage - don't [smile]

The task manager's memory usage is a bit more subtle that just memory allocated by your app.
Quote:Original post by Evil Steve
If you're using the task manager to watch your app's memory usage - don't [smile]

The task manager's memory usage is a bit more subtle that just memory allocated by your app.


i m using the task manager to watch my app's memory usage !

before i launch the program 288mb are used.
next i launch the program 299mb.
i use deleteGlutWindow 298mb
i close the program 288mb.

Here my problem : The user will be able to open and close the window hundreds of time, if each time 10mb are lost, it's a big problem for me.

thx for your help !


edit : It seems that when using C++, the simplest solution to this problem is to wrap your GLUT application inside of a C++ class and create it with global scope. The C++ language guarantees that the class' destructor is called when the object goes out of scope. ( from opengl faq )

i must try that solution !
How do you know that those 10 MB is because of the window, and not used by application itself? You know that by starting the application and creating a window, 10 MB is used somwehere. That's two things that can use memory, but you don't know for sure it's the window. Check the usage after starting the application, but before creating the window.

As you can see, creating a window adds 11 MB, and releases 1 MB when destroying it. If by just starting the application and not creating any windows you use 10 MB, you don't have any problem.
Quote:Original post by Brother Bob
How do you know that those 10 MB is because of the window, and not used by application itself? You know that by starting the application and creating a window, 10 MB is used somwehere. That's two things that can use memory, but you don't know for sure it's the window. Check the usage after starting the application, but before creating the window.

As you can see, creating a window adds 11 MB, and releases 1 MB when destroying it. If by just starting the application and not creating any windows you use 10 MB, you don't have any problem.


i add "char c=getch();" at the beginning of the main function to look at what you said.

memory usage : 391
i start the program : 392
push a key to create the window : 402
destroy the window : 401
close the program : 391

you can test it if you want.
The Task Manager is completely useless for this sort of thing. It doesn't tell you how much memory your app is using at all. What it tells you is your applications working set size. When you free up memory using free(), delete, or whatever, it doesn't immediately get freed to the OS. the application has a cache of memory it uses, and it'll only request memory from the OS when it runs out. It won't free it back to the OS unless it has loads spare.

In short: Ignore the task manager's memory usage completely - it means nothing. If you want to check for memory leaks, use something like the CRT memory tracking functions or a memory manager like mmgr.
Quote:Original post by Evil Steve
The Task Manager is completely useless for this sort of thing. It doesn't tell you how much memory your app is using at all. What it tells you is your applications working set size. When you free up memory using free(), delete, or whatever, it doesn't immediately get freed to the OS. the application has a cache of memory it uses, and it'll only request memory from the OS when it runs out. It won't free it back to the OS unless it has loads spare.

In short: Ignore the task manager's memory usage completely - it means nothing. If you want to check for memory leaks, use something like the CRT memory tracking functions or a memory manager like mmgr.


ok, i will try mmgr. Thx for your help !

This topic is closed to new replies.

Advertisement