我正在使用SDL2和C ++(使用g ++编译并使用valgrind来调试某些东西),现在我使用包装类为渲染器,Windows等设置析构函数和构造函数。我的程序是故意崩溃和段错误,但有时只是。它大部分时间都没有,但很可能是由于未初始化的变量。这是valgrind所说的:
==227== Conditional jump or move depends on uninitialised value(s)
==227== at 0xEA89BF2: ??? (in /usr/lib/x86_64-linux-gnu/dri/swrast_dri.so)
==227== by 0xEA89CB3: ??? (in /usr/lib/x86_64-linux-gnu/dri/swrast_dri.so)
==227== by 0x4E9A920: ??? (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.4.0)
==227== by 0x4E9A978: ??? (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.4.0)
==227== by 0x4E9BA62: ??? (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.4.0)
==227== by 0x4E8FE12: ??? (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.4.0)
==227== by 0x4032E9: game::Renderer::init() (Renderer.cpp:13)
==227== by 0x4033CA: game::Renderer::Renderer(SDL_Window*&) (Renderer.cpp:31)
==227== by 0x401AA2: game::Environment::Environment() (Environment.cpp:11)
==227== by 0x4015D0: main (source.cpp:15)
==227== Uninitialised value was created by a stack allocation
==227== at 0x4E9A7A0: ??? (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.4.0)
这里的渲染器功能有问题:
namespace game {
//init function that sets up the renderer. It is basically so all of my constructors don't use the same
//code.
void Renderer::init() {
if (window != NULL) {
theRenderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED);
if (theRenderer == NULL) {
cout << "Renderer could not be created: " <<
SDL_GetError() << endl;
}
else {
SDL_SetRenderDrawColor (theRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
}
}
}
//parameterized constructor that takes a reference to an SDL_Window pointer
//and sets up the renderer for that window
Renderer::Renderer(SDL_Window*& initWindow): theRenderer(NULL), window(initWindow) {
this->init();
}
}
应该初始化它的窗口构造函数:
namespace game {
//Default constructor that sets up the window to be rendered to
Window::Window(): theWindow(NULL) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl;
}
else {
this->theWindow = SDL_CreateWindow("Platformer", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(theWindow == NULL) {
cout << "Window could not be created! SDL_Error: "
<< SDL_GetError() << endl;
}
}
}
}
我已经四处搜索,但无法找到与此特定错误相关的任何内容。有谁看到我未初始化的变量在哪里?