2012-10-19 122 views
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.(不是聽起來很有道理...) :)QThread崩潰程序?

我的代碼:

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



}; 

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

主窗口(主窗口有背景*作爲財產)

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(); 
} 
+0

在哪裏做你的調試器說,它已經崩潰? – cmannett85

+0

任何地方,差不多。不一樣的地方。通常當線程等待鎖(我發現它不是通過調試器打印出來的)... :) – Al2O3

+0

現在我真的懷疑我的程序中有一個QTimer(在MainWindow中)即使我沒有將它連接到任何SLOT,但'timer =新的QTimer()'和'timer-> start(1000)'會使程序崩潰,但是一切似乎都適用於兩個句子評論... :) – Al2O3

回答

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個字節的數組。
各類事故的出現,這是混亂和令人沮喪... :(:(:(