所以使用即時通訊和GLFW以下方法作品時叫我的主要方法線程化該方法會產生段錯誤,爲什麼?
void Display::run() {
while (!glfwWindowShouldClose(window))
{
/* Render here */
/* Swap Buffers And Poll */
glfwSwapBuffers(window);
glfwPollEvents();
}
}
但是當我嘗試在一個單獨的線程中運行它,我得到一個segfault
std::thread t1(&Display::run, this);
什麼想法?詢問是否要更多的代碼
編輯的: 的main.cpp
#include "src/support/Display.h"
int main() {
Display* d;
d->start();
return 0;
}
Display.h
#include <GLFW/glfw3.h>
#include <exception>
#include <thread>
class Display {
private:
GLFWwindow* window;
std::thread* displayThread;
void run();
public:
Display();
void start();
void close();
};
/* Exceptions */
struct GLFWNotInitilizedException : public std::exception
{
const char * what() const throw()
{
return "ERR: Could Not Initialize GLFW";
}
};
struct WindowNotCreatedException : public std::exception
{
const char * what() const throw()
{
return "ERR: Could Not Create Window";
}
};
Display.cpp
#include "Display.h"
Display::Display() {
//init glfw
if (!glfwInit())
throw GLFWNotInitilizedException();
//create window and its context
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
throw WindowNotCreatedException();
}
//make windows context current
glfwMakeContextCurrent(window);
//run(); //this works instead of calling start() which puts run() into a thread
}
/* begins the rendering of the display window contents in seperate thread */
void Display::start() {
std::thread t1(&Display::run, this);
displayThread = &t1;
}
/* renders contents of display window */
void Display::run() {
while (!glfwWindowShouldClose(window)) //seg fault is output here
{
/* Render here */
/* Swap Buffers And Poll */
glfwSwapBuffers(window);
glfwPollEvents();
}
}
/* to be used when closing display window */
void Display::close() {
glfwSetWindowShouldClose(window, true);
displayThread->join();
glfwDestroyWindow(window);
}
我們不應該要求查看更多的代碼,請閱讀http://stackoverflow.com/help/mcve和http://kera.name/articles/2013/10/nobody-writes-testcases-任何更多/和http://blog.jerryorr.com/2014/04/solve-your-problem-by-almost-asking.html –
@JonathanWakely希望這會讓它更清晰,對不起,如果我沒有問正確的方式,因爲我從來不曾訴諸詢問本網站 –
d-> start();使用垃圾指針,你沒有指向任何東西 –