c++ - SDL/OpenGL 双缓冲内存泄漏

标签 c++ opengl memory-leaks sdl

所以我一直在尝试使用 SDL 和 OpenGL 为游戏制作我自己的迷你引擎,主要游戏逻辑包含在单例类中。但是对于我的生活,我无法弄清楚我是如何在引擎的这个示例部分发生内存泄漏的,尽管我很确定它会在我调用 SDL_GL_SwapBuffers 时发生。每隔两三秒泄漏一次大约 4 K。我正在使用 SDL 1.3;请帮我找到漏洞,过去一周这让我抓狂!

主模块.h

#ifndef MAINMODULE_H
#define MAINMODULE_H

/// Includes
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"
#include <string>

/// Define Statements (Screen elements)
#define MAINMODULE_WIDTH 800
#define MAINMODULE_HEIGHT 600
#define MAINMODULE_CAPTION "MainModule"

/// Define Statements (OpenGL memory usage)
#define MAINMODULE_RED_SIZE 8
#define MAINMODULE_GREEN_SIZE 8
#define MAINMODULE_BLUE_SIZE 8
#define MAINMODULE_ALPHA_SIZE 8
#define MAINMODULE_BUFFER_SIZE 32
#define MAINMODULE_DEPTH_SIZE 16
#define MAINMODULE_DOUBLEBUFFER 1 // 1 to Enable
#define MAINMODULE_FLAGS SDL_OPENGL

/// Define Statements (OpenGL elements)
#define MAINMODULE_CLEARCOLOR 1.0f, 1.0f, 1.0f, 1.0f
#define MAINMODULE_SHADEMODEL GL_SMOOTH
#define MAINMODULE_DEPTH_TEST 0 // 1 to Enable

class MainModule {

    private: // Constructor/Deconsctuctor

        MainModule();
        ~MainModule();

    private: // Class Variables

        static MainModule* _Instance;       // Singleton instance of the module.

        static int _Width;                  // Width of the game screen.
        static int _Height;                 // Height of the game screen.
        static std::string _Caption;        // Game screen caption/title.
        static SDL_Surface* _ScreenSurface; // Game screen as represented by the window.
        static SDL_Event* _Event;   // Events such as mouse/key input.

        static bool _IsInitialized;         // Has the engine been initialized?
        static bool _IsRunning;             // Is the engine running?

    public: // Get/Set Functions

        static inline int Width() { return _Width; }
        static inline int Height() { return _Height; }
        static inline std::string Caption() { return _Caption; }

        static inline bool IsInitialized() { return _IsInitialized; }
        static inline bool IsRunning() { return _IsRunning; }

        static void SetCaption(std::string caption);

    public: // Class Functions

        static void ConstructInstance();
        static void DeconstructInstance();

        static void InitializeModule();
        static void RunGameLogic();                         // Updates and renders game information.

};

#endif // MAINMODULE_H

主模块.ccp

/// Includes
#include "MainModule.h"
#include <iostream>

// Static Variable Declarations
MainModule* MainModule::_Instance = 0;
int MainModule::_Width = MAINMODULE_WIDTH;
int MainModule::_Height = MAINMODULE_HEIGHT;
std::string MainModule::_Caption = MAINMODULE_CAPTION;
SDL_Surface* MainModule::_ScreenSurface = 0;
SDL_Event* MainModule::_Event = 0;
bool MainModule::_IsInitialized = false;
bool MainModule::_IsRunning = false;

/// Constructor/Deconstructor
MainModule::MainModule() { }

MainModule::~MainModule() { 
    if (_Event != 0) delete _Event;
    if (_ScreenSurface != 0) SDL_FreeSurface(_ScreenSurface);
    SDL_Quit();
}

/// Set Functions
void MainModule::SetCaption(std::string caption)
{ _Caption = caption; if (_IsInitialized) SDL_WM_SetCaption(_Caption.c_str(), 0); }

/// Class Functions
void MainModule::ConstructInstance()
{ if (_Instance == 0) _Instance = new MainModule(); }

void MainModule::DeconstructInstance()
{ if (_Instance != 0) { delete _Instance; _Instance = 0; } }

void MainModule::InitializeModule() {
    ConstructInstance(); // Create an instance if the ConstructInstance function wasn't created before.
    if (_Instance == 0) { printf("MainModule instance not created properly./n"); return; } 

    // Initialize SDL.
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { printf("SDL Initialization error: %s/n", SDL_GetError()); return; }

    // Set OpenGL memory usage.
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, MAINMODULE_RED_SIZE);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, MAINMODULE_GREEN_SIZE);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, MAINMODULE_BLUE_SIZE);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, MAINMODULE_ALPHA_SIZE);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, MAINMODULE_BUFFER_SIZE);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, MAINMODULE_DEPTH_SIZE);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, MAINMODULE_DOUBLEBUFFER);

    // Creates the screen and window.
    _ScreenSurface = SDL_SetVideoMode(MAINMODULE_WIDTH, MAINMODULE_HEIGHT, MAINMODULE_BUFFER_SIZE, MAINMODULE_FLAGS);
    if (_ScreenSurface == 0) { printf("ScreenSurface not created properly./n"); return; }
    SDL_WM_SetCaption(_Caption.c_str(), 0);

    (MAINMODULE_DEPTH_TEST == 1) ? glEnable(GL_DEPTH_TEST) : glDisable(GL_DEPTH_TEST);

    glClearColor(MAINMODULE_CLEARCOLOR);
    glShadeModel(GL_SMOOTH);

    _IsInitialized = true;
    _IsRunning = true;
    _Event = new SDL_Event();
}

void MainModule::RunGameLogic() {
    while (SDL_PollEvent(_Event)) { // Event handling loop
        switch (_Event->type) {
            case SDL_QUIT: // Exits out of game
            { _IsRunning = false; break; }
            case SDL_ACTIVEEVENT:
            { break; }
            case SDL_KEYDOWN: // Keyboard press down
            { break; }
            case SDL_KEYUP: // Keyboard press up
            { break; }
            case SDL_MOUSEMOTION: // Mouse movement
            { break; }
            case SDL_MOUSEBUTTONDOWN: // Mouse button down
            { break; }
            case SDL_MOUSEBUTTONUP: // Mouse button up
            { break; }
        }
    }

    // Rendering logic
    (MAINMODULE_DEPTH_TEST == 1) ? glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) : glClear(GL_COLOR_BUFFER_BIT);

    SDL_GL_SwapBuffers();
}

/// Entry point for the program
int main(int argc, char** argv) {
    MainModule::InitializeModule();
    if (MainModule::IsInitialized()) { // If the modules initialized sucessfully
        while (MainModule::IsRunning()) { MainModule::RunGameLogic(); }
    }
    MainModule::DeconstructInstance();

    return 0;
}

最佳答案

一般来说,此类问题与底层 OpenGL 库、编译器或类似内容的内部问题有关 - 像这样的图形代码是 notorious因为有点马车和挑剔。

如果您提供更多详细信息,我可能会提供更多帮助 - 什么操作系统、编译器、图形驱动程序/版本、要构建的特定 SDL 版本等。

与此同时,考虑在另一个操作系统下编译,看看会发生什么,或者切换出 SDL 版本。

但是,是的,几乎可以肯定你没有做错任何事......

关于c++ - SDL/OpenGL 双缓冲内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6147166/

相关文章:

c++ - 在 C++ 中获取 double 的精确位表示

c++ - 具有多个阴影的阴影贴图

c++ - 冗余顶点缓冲区导致 OpenGL 无法正确绘制

c++ - C/C++ 中的#error 是如何工作的?

c++ - 使用 automake 将子目录编译到库中的最佳方法?

c - 分割和复制字符串导致内存泄漏

c++ - atldbsch.h 中的 BSTR 内存泄漏 - 怎么办?

ios - 我该如何解决这个漏洞?

c++ - 从 C++ Windows 应用商店应用程序使用 sqlite-winrt

c++ - 旋转两个球体