线程化此方法会产生段错误,为什么? [英] threading this method produces a segfault, why?
问题描述
因此,即时通讯使用GLFW,并且在我的主要方法中调用时,以下方法可以正常工作
So im using GLFW and the following method works when called in my main method
void Display::run() {
while (!glfwWindowShouldClose(window))
{
/* Render here */
/* Swap Buffers And Poll */
glfwSwapBuffers(window);
glfwPollEvents();
}
}
但是当我尝试在单独的线程上运行它时,我会遇到段错误
but when i try to run it on a separate thread i get a segfault
std::thread t1(&Display::run, this);
有什么想法吗?询问是否需要更多代码
any ideas? ask if you want any more of the code
main.cpp
#include "src/support/Display.h"
int main() {
Display* d;
d->start();
return 0;
}
Display.h
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
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);
}
推荐答案
Display* d;
您尚未在此处创建对象,只是未初始化的指针.
You haven't created an object here, just an uninitialised pointer.
d->start();
这将调用不存在的对象上的成员.当它尝试访问run()
函数中的任何成员时,它只是访问垃圾,因为没有对象.
This calls a member on a nonexistent object. When it tries to access any members in the run()
function it just accesses garbage because there is no object.
您可能想要创建一个像这样的对象:
You probably want to create an object like this:
Display d;
d.start();
此外,您的启动函数将终止程序,因为您在销毁线程之前不加入该线程.在尝试使用此类线程和指针之前,您应该了解C ++对象生存期的基础.
Also your start function will terminate the program because you don't join the thread before it is destroyed. You should learn the basics of C++ object lifetime before trying to use threads and pointers like this.
停止使用指针,直到您了解基本知识为止. displayThread
应该是实际的std::thread
,而不仅仅是指向超出范围的某些std::thread
的指针.
Stop using pointers until you understand the basics. displayThread
should be an actual std::thread
not just a pointer to some std::thread
that goes out of scope.
然后您可以做:
void Display::start() {
displayThread = std::thread(&Display::run, this);
}
请确保在销毁displayThread.join()
之前调用它,例如在Display
析构函数中.
Make sure you call displayThread.join()
before it is destroyed, e.g in the Display
destructor.
这篇关于线程化此方法会产生段错误,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!