0

我像这样实现 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. (听起来不合理......) :)

我的代码:

背景.h

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();



};

背景.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();
}
4

1 回答 1

0

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

#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 个字节。
各种崩溃出现,令人困惑和沮丧...... :( :( :(

于 2012-10-20T02:40:18.563 回答