QT程序崩潰原因示例和分析

崩潰示例

程序崩潰的一般原因

一、內存訪問

QT中的QList和他的了類QByteArrayList,QItemSelection,QQueue,QStringList以下標方式引用時,如果未對下標是由越界進行檢查,就有可能會引起程序的崩潰。

1.1 SIMIS-Teacher CreateUser.cpp

數組越界,引起崩潰:

另外還需要注意:ui.comboBox_structure->currentIndex()在QT控件獲取選中位置時,在未選中時多數返回-1,如果未加檢查就在數組中引用就會引起問題。

    _map[DB_Field_role] = m_bTeacher ? _Role_Teac : _Role_Stu;

    _map[DB_Field_ParentID] = m_classIDList.at(ui.comboBox_structure->currentIndex());

 

修改後代碼:

    _map[DB_Field_role] = m_bTeacher ? _Role_Teac : _Role_Stu;

const int ncIndex = ui.comboBox_structure->currentIndex();

    if((0 <= ncIndex) && (m_classIDList.size() > ncIndex)){

        _map[DB_Field_ParentID] = m_classIDList.at(ncIndex);/

    }

1.2 UserInforManager  UserInforManager.cpp

數組越界,引起崩潰:

QVariantMap _user = _users[0];

              _userRes << _user;

              role = _user.value(DB_Field_role).toString();

 

修改爲:

QVariantMap _user;

              if (0 < _users.size()) {

                     _user = _users[0];

                     _userRes << _user;

                     role = _user.value(DB_Field_role).toString();

              }

 

 

1.3 UserInforManager  UserInforManager.cpp

數組越界,引起崩潰:

_oldMap = QueryUserInfor(getAccount(userID.toInt())).at(0);

 

修改爲:

auto qlmTemp = QueryUserInfor(rDataBase, getAccount(rDataBase, userID.toInt()));

              if (0 < qlmTemp.size()) {

                     _oldMap = qlmTemp[0];

              }

1.4 數據溢出

char buffer[10];

int counter;

lstrcpy(buffer, "abcdefghik");

 

在debug版中buffer的結束標誌覆蓋了counter的高位,但是除非counter的數值大於16M,否則不會引起問題。但是在release版中,counter可能被放在寄存器中,這樣NULL就覆蓋了buffer下面的空間,可能就是函數的返回地址,這將導致ACCESS ERROR。

二、使用無效的迭代器

2.1 引用無效的iterator

Erase操作作迭代器it已經效,對其操作會引起未定義的問題。

    if (!bExist)//不存在刪除

        {      

vAllSelPackageUUID.erase(it);

   

            QString strTip = QString(QStringLiteral("文件%1不存在是否刪除配置信息?")).arg(strFilePath);

            if (QMessageBox::Ok == QMessageBox::warning(NULL, QStringLiteral("提示"), strTip, QMessageBox::Ok | QMessageBox::Cancel))

            {

                QString strConfigFile = ModifyPath(Cls_PackageDataInfo::GetInstance()->GetWorkPath()) + CONST_QSTR_SERVER_CONFIG_FILE;

                Cls_PackageDataInfo::GetInstance()->GetPackageDataInfoFromServerByID(*it)->DelPackageConfig(strConfigFile);

                bUpdateServerFile = true;

            }

 

                        continue;

        }

建議修改爲:

    if (!bExist)//不存在刪除

        {          

            QString strTip = QString(QStringLiteral("文件%1不存在是否刪除配置信息?")).arg(strFilePath);

            if (QMessageBox::Ok == QMessageBox::warning(NULL, QStringLiteral("提示"), strTip, QMessageBox::Ok | QMessageBox::Cancel))

            {

                QString strConfigFile = ModifyPath(Cls_PackageDataInfo::GetInstance()->GetWorkPath()) + CONST_QSTR_SERVER_CONFIG_FILE;

                Cls_PackageDataInfo::GetInstance()->GetPackageDataInfoFromServerByID(*it)->DelPackageConfig(strConfigFile);

                bUpdateServerFile = true;

            }

vAllSelPackageUUID.erase(it);

 

                        continue;

        }

三、雜項

3.1 Assert問題

ASSERT在Release版本中是不會被編譯的。 假如你在這些語句中加了程序中必須要有的代碼。例:

ASSERT(pNewObj = new CMyClass);

pNewObj->MyFunction();

這種時候Release版本中的pNewObj不會分配到空間,所以Assert中的代碼,不能有邊際效應。

3.2 庫版本

人們將不同版本DLL混合造成的不一致性形象的稱爲 “動態連接庫的地獄“(DLL Hell)。不能將debug和release版的DLL混合在一起使用。debug都是debug版,release版都是release版。

debug版和release版會引起難查找的問題。

3.3 多線程

在多線程的環境下,對於關鍵資源需要加鎖,特別是在對容器變量進行修改時。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章