OpenGL記憶體流失之主迴圈函數glutMainLoop()

來源:互聯網
上載者:User

 

      在Win32控制台程式中,標準的OpenGL流程是這樣的: 
int main(int argc, char *argv[])
{

  glutInitWindowSize(500, 250);
  glutInitWindowPosition ( 140, 140 );
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
  glutInit(&argc, argv);

 

 

  glutCreateWindow( "Demo" );

 

  glClearColor(1.0, 1.0, 1.0, 1.0);

  glutDisplayFunc( Display );
  glutReshapeFunc( Reshape );

 

  glutMouseFunc ( Mouse ) ;
  glutMotionFunc ( Motion ) ;

 

  glutKeyboardFunc( Key );

 

  glutMainLoop();

  return 0;

}

 

而在MFC的對話方塊程式中,使用這樣的流程,可能會出現記憶體的泄漏的問題,究其原因,是因為glutMainLoop()  阻斷了系統的正常流程,在關閉glutCreateWindow()建立的視窗後,glutMainLoop()直接用exit(0)退出程式,而不會繼續執行glutMainLoop()後的語句,這就使一些類的釋放及資源回收工作無法進行,從而造成記憶體流失。糾正此問題,有3個辦法:

1、使用glutLeaveMainLoop()代替glutMainLoop()。

2、處理代碼中exit(0)的部分。

3、在glutMainLoop之前先設定:

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);

然後再用glutMainLoop();則 glutMainLoop()會在退出後,繼續執行其後的代碼。

使用以上方法,可以有效處理glutMainLoop()造成的記憶體流失!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.