gpt4 book ai didi

multithreading - QThread使程序崩溃?

转载 作者:行者123 更新时间:2023-12-03 17:39:54 24 4
gpt4 key购买 nike

我实现了这样的QThread,但是在运行时让程序崩溃了。
我已经搜索并看到帖子说,这不是使用QThread的正确方法。
但是我找不到程序崩溃的任何原因,我所做的只是
触发“on_Create_triggered()”,我保证互斥锁已正确锁定和解锁。
我已经对该程序进行了两天的测试(仅通过'std::cerr << ...;'进行测试才能显示结果),但是仍然找不到原因。 What I guess is that the thread may wait for the lock too long and cause program to crash.(听起来不合理...):)

我的代码:

后台

class Background : public QThread
{
Q_OBJECT

public:
Background(int& val,DEVMAP& map, QQueue<LogInfoItem*>& queue, QList<DEV*>& devlist, QList<IconLabel*>& icllist,QMutex& m)
:val_i(val),DevMap(map), LogInfoQueue(queue), DevInfoList(devlist), IconLabelList(icllist),mutex(m)
{}

~Background();

protected:
void run(void);


private:
DEVMAP& DevMap;
QQueue<LogInfoItem*>&LogInfoQueue;
QList<DEV*>& DevInfoList;
QList<IconLabel*>& IconLabelList;
int& val_i;
QMutex& mutex;



void rcv();



};

Background.cpp
#include "background.h"


Background::~Background()
{
LogFile->close();
}

void Background::run(void)
{
initFile();

while(1)
{
msleep(5);
rcv();
}

}


void Background::rcv()
{
mutex.lock();
...
...//access DevMap, LogInfoQueue, DevInfoList, IconLabelList and val_i;
...
mutex.unlock();
}

MainWindow:(MainWindow具有Background *作为属性)
void MainWindow::initThread()
{
back = new Background(val_i, dev_map, logDisplayQueue, devInfoList, iconLabelList, mutex);
back->start();
}

void MainWindow::on_Create_triggered()
{
mutex.lock();
...
...//access DevMap, LogInfoQueue, DevInfoList, IconLabelList and val_i;
...
mutex.unlock();
}

最佳答案

我已经找到了原因,这更加微妙。
(我使用其他人编写的一些代码,但相信它没有坏,我完全错了!:))
损坏的代码:

#define   DATABUFLEN    96

typedef struct Para//totally 100bytes
{
UINT8 type;
UINT8 len;
UINT8 inType;
UINT8 inLen;
UINT8 value[DATABUFLEN];//96 bytes here
}ERRORTLV;


class BitState
{

public:
UINT8 dataBuf[DATABUFLEN];
......

};

和使用它的功能:
bool    BitState::rcvData()               //the function crosses bound of array
{

UINT8 data[12] =
{
0x72, 0x0A, 0x97, 0x08,
0x06, 0x0A, 0x0C, 0x0F,
0x1E, 0x2A, 0x50, 0x5F,
}; //only 12 bytes

UINT32 dataLen = 110;

memcpy(this->dataBuf, data, dataLen); //copy 110 bytes to dataBuf //but no error or warning from compiler, and no runtime error indicates the cross
}


bool BitState::parseData(BitLog* bitLog)//pass pointer of dataBuf to para_tmp, but only use 0x08 + 4 = 12 bytes of dataBuf
{


Para* para_tmp;
if(*(this->dataBuf) == 0x77)
{
para_tmp = (ERRORTLV*)this->dataBuf;
}

if(para_tmp->type != 0x72 || para_tmp->inType != 0x97 || (para_tmp->len - para_tmp->inLen) != 2) // inLen == 0x08
{
return false;
}
else
{
//parse dataBuf according to Para's structure


this->bitState.reset();


for(int i = 0; i < para_tmp->inLen; i++) // inLen == 0x08 only !!!
{
this->bitState[para_tmp->value[i]-6] = 1;
}


if(this->bitState.none())
this->setState(NORMAL);
else
this->setState(FAULT);

QString currentTime = (QDateTime::currentDateTime()).toString("yyyy.MM.dd hh:mm:ss.zzz");

string sysTime = string((const char *)currentTime.toLocal8Bit());

this->setCurTime(sysTime);

this->addLog(sysTime, bitLog);

}

return true;
}



bool BitState::addLog(std::string sysTime, BitLog* bitLog)// this function is right
{

bitLog->basicInfo = this->basicInfo;//not in data Buf, already allocated and initialized, (right)
bitLog->bitState = this->bitState; //state is set by setState(..)

bitLog->rcvTime = sysTime; //time

return true;
}

通常来说,该程序将96个字节分配给一个字节数组,但是使用'memcpy(...)'将110个字节复制到该数组,以后仅使用该数组的12个字节。
出现各种崩溃,这令人困惑和沮丧... :( :( :(

关于multithreading - QThread使程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12976907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com