所以我一直在努力为使用SDL和OpenGL的游戏制作我自己的迷你引擎,主游戏逻辑包含在单例类中。但是对于我的生活,我无法弄清楚我是如何通过引擎的这个示例部分获得内存泄漏的,尽管我很确定它会在我调用SDL_GL_SwapBuffers时发生。每隔几两三秒泄漏约4K。我正在使用SDL 1.3;请帮我找一下泄漏,过去一周让我发疯了!
#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
/// 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;
}
答案 0 :(得分:0)
一般来说,这类问题与底层OpenGL库,编译器或类似内容的内部问题有关 - 像notorious这样的图形代码有点儿错误和挑剔。
如果您提供更多详细信息,我可以提供更多帮助 - 操作系统,编译器,图形驱动程序/版本,要构建的特定SDL版本等等。
与此同时,请考虑在其他操作系统下进行编译,看看会发生什么,或者切换出SDL版本。
但是,几乎可以肯定你没有做错任何事......