<分区>
我有一个 QThread 正在运行一些代码,我希望它能够很好地退出并进行一些清理,因此代码如下:
测试设备.h
class testDevice : public QThread
{
Q_OBJECT
... // some definitions
protected:
void run(void);
private:
hid_device *handle;
bool abort;
public:
~testDevice(void);
};
测试设备.cpp
testDevice::~testDevice(void)
{
mutex.lock();
abort = true;
mutex.unlock();
wait();
if (handle != NULL)
{
hid_close(handle);
}
hid_exit();
}
void testDevice::run(void)
{
while(true)
{
mutex.lock();
if(abort)
{
return;
}
mutex.unlock();
if (_connected)
{
//... more code
}
else // Case device is not connected
{
emit deviceConnected(_connected);// A breakpoint in here is not triggered after abort is true
handle = hid_open(TEST_VID, TEST_PID, NULL); // this is where VS2010 shows the error "Unhandled exception at 0x71241a95 in project.exe: 0xC0000005: Access violation."
//...code continues
我期待的行为是 run()
被调用,当 ~testDevice()
析构函数被调用时 abort
被设置为 true
和 wait
阻塞析构函数,run
返回然后析构函数继续。
发生的事情是 run()
被调用,当我关闭应用程序时,析构函数 ~testDevice()
被调用并且 abort
设置为 true
并且 wait()
返回并且析构函数完成...但是 run()
继续运行并且我在project.exe 中的 0x71241a95:0xC0000005:访问冲突......我正在将其作为 VS2010 调试运行,如果我放置断点,我会一直得到这个,但是没有断点我只是偶尔得到这个......任何线索?
奇怪的是,当我在 abort = true;
中放置一个断点时,有时第一个到达那里的断点会有一个蓝色的小 '!'。标志,它第二次击中是一个普通的全红色标志。 emit deviceConnected(_connected);
处的断点也会发生同样的情况,但这也有点随机......我不知道这是什么'!'意味着......所有这些都可能是调试问题吗?
我的一般怀疑是 HIDAPI 在单独的线程中运行,当有人调用 hid_exit();
时它有自己的错误,也许 hid_read();
继续运行由于 hid_quit();
被调用,hid_read();
丢失了一些指针并且没有关闭......只是一个疯狂的镜头并确认这一点我需要一些开发人员HIDAPI 站出来说点什么……我希望他们能读到这篇文章