所以我正在使用C ++开发3D游戏引擎/ api。但我遇到过一个我从未真正得到的错误。分段错误,现在通过一些研究我能够理解这是什么(或者至少我以为我做了,也许我不理解它,这就是为什么我继续有错误)。无论如何,当我尝试使用我的指针对象时,它会崩溃并出现错误:
void RenderEngine::Render(GameObject* object)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
std::cout << "debug 1\n"; //this prints
object->RenderAll(m_defaultShader, this); //segmentation fault occurs here
std::cout << "debug 2\n"; //this does not get printed
...more code...
}
我用这个引用了这个函数/方法:
void Game::Render(RenderEngine* renderEngine)
{
renderEngine->Render(&m_root); //m_root is declared as 'GameObject m_root;'
}
我已经检查过m_root是否为null,它不是! 这也是我第一次使用StackOverflow,所以如果格式化或任何东西很奇怪我道歉。
如果有人能帮助我理解为什么我在做什么不起作用,那就会挽救生命。如果它有任何机会与我的操作系统或编译器有任何关系,我在Linux Mint上使用Code :: Blocks和GNU GCC编译器。
编辑:
因此,经过进一步的调试和更多的拍打假人,我意识到我没有正确初始化我的游戏,所以我添加了函数调用,现在就是发生崩溃的地方:
引擎构造函数:
Engine::Engine(int width, int height, double framerate, Game* game) :
m_running(false),
m_width(width),
m_height(height),
m_frameTime(1.0 / framerate),
m_game(game),
m_renderEngine(NULL)
{
m_game->SetEngine(this);
}
m_game-&gt;在循环中调用Init():
...code
printf("%s\n", "sef"); //prints
m_game->Init(); //segmentation fault crash
printf("%s\n", "s324"); //does not print
...code
主要功能:
int main()
{
Test game;
Engine engine(800, 600, 60, &game);
engine.CreateWindow("Boss3D Engine");
engine.Start();
return 0;
}
这是Start和CreateWindow函数:
void Engine::Start()
{
if(m_running) return;
Run(); // this contains the game loop of the engine, the first lines set m_running to true, then try to call the m_game->Init like above, so it doesn't even complete one iteration
}
void Engine::CreateWindow(const std::string& title)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
SDLCreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, false);
GLenum res = glewInit();
if(res != GLEW_OK)
{
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_CLAMP);
}
SDLCreateWindow:
void SDLCreateWindow(const char* title, int x, int y, int width, int height, bool fullscreen)
{
int mode = 0;
if(fullscreen)
mode = SDL_WINDOW_FULLSCREEN;
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | mode);
glContext = SDL_GL_CreateContext(window);
//SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
SDL_GL_SetSwapInterval(1);
}
我使用了valgrind工具并将结果放在一个文本文件中,但是它太长了以至于我无法粘贴到任何地方而且之前从未使用过valgrind因此我不确定如何解释结果,这里是文件:valgrind file
经过几个小时的放置许多打印后,我发现这是真正崩溃的地方:
inline void AddLight(BaseLight* light)
{
std::cout << "adding a light\n";
std::cout << light << std::endl;
//crash right below this line
std::cout << "m_lights size: " << m_lights.size() << std::endl; //not printed
m_lights.push_back(light);
std::cout << "New m_lights size: " << m_lights.size() << std::endl;
}
此功能在首次尝试使用时崩溃,m_lights声明为:std::vector<BaseLight*> m_lights;
答案 0 :(得分:0)
解决方案供将来参考:为什么这会影响它我不太确定,但是我通过几个小时的调试和更改用于编译代码的终端命令解决了这个问题,我现在使用gnome-terminal --disable-factory -t $TITLE -x