QT在跑步过程中崩溃

QT crashed during running

本文关键字:过程中 崩溃 QT      更新时间:2023-10-16

我正在尝试将一些数据从文件加载到嵌套列表中。但是,应用程序在加载数据期间崩溃。我发现代码在将数据附加到我最里面的列表中时崩溃了。有人可以帮助我了解发生了什么以及如何解决它吗?谢谢。

头文件(snp_plotter.h(

#ifndef SNP_PLOTTER_H
#define SNP_PLOTTER_H
#include <QMainWindow>
#include "QList"
#include <QtCharts/QChartGlobal>
QT_CHARTS_BEGIN_NAMESPACE
class QLineSeries;
class QChart;
QT_CHARTS_END_NAMESPACE
QT_CHARTS_USE_NAMESPACE
namespace Ui {
class SNP_Plotter;
}
class SNP_Plotter : public QMainWindow
{
Q_OBJECT
public:
explicit SNP_Plotter(QWidget *parent = 0);
~SNP_Plotter();
private slots:
void on_openSnpFile_clicked();
private:
Ui::SNP_Plotter *ui;
QString nPorts;
QString currentFile;
QString snpData;
QString snpHeader;
QString domainType; //freq or time
QString measureMethod; // s,y,abcd, etc
QString measureType; // real+img, log+angl or linear+angl
QString referenceImp; // system impd
QList<QVector<double>*> listVector; //data list with [[][][]]
};
#endif // SNP_PLOTTER_H

代码(主.cpp(:

#include "snp_plotter.h"
#include "ui_snp_plotter.h"
#include "QFile"
#include "QFileDialog"
#include "QTextStream"
#include "QMessageBox"
#include "QDebug"
#include "QRegularExpression"
#include "QRegularExpressionMatch"
#include <QQueue>
#include <QList>
#include <QList>
SNP_Plotter::SNP_Plotter(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::SNP_Plotter)
{
ui->setupUi(this);
this->setCentralWidget(ui->verticalLayoutWidget);
}
SNP_Plotter::~SNP_Plotter()
{
delete ui;
}
void SNP_Plotter::on_openSnpFile_clicked()
{
// clean up the currntfile cache
QString openFile = QFileDialog::getOpenFileName(this,
tr("Open SNP file"),
"/usr",
"SNP(*.s*p)"
);
//extract number of ports
QRegularExpression snpFormat(".s(\d+)p");
QRegularExpressionMatch match0 = snpFormat.match(openFile);
nPorts = match0.captured(1);
qint8 numberItems = 2*(nPorts.toInt()*nPorts.toInt())+1;
//    listVector = new QList<QList<double>*>(numberItems);
for(auto i=0; i<numberItems; i++){
QVector<double> *vector = new QVector<double>(1000);
listVector.push_back(vector);
}
//open file
QFile file(openFile);
if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", "Cannot open file: " + file.errorString());
return;
}

QTextStream dataReading(&file);
qint8 counter = 0;
QRegularExpression headerFormat("#\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)");
QRegularExpression dataFormat("([^ ][-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)?)");
QString currentLine;
while(!dataReading.atEnd()){
//start a new line in the file
currentLine = dataReading.readLine();
//type of lines to skip
if((currentLine[0]=="x0")||(currentLine[0]=="!") ){continue;}
//data extraction and insertion
else if((currentLine[0]!="#")&&(currentLine[0]!="!")){
QRegularExpressionMatchIterator dataList = dataFormat.globalMatch(currentLine);
while(dataList.hasNext()){
QRegularExpressionMatch matchedData = dataList.next();
QString data = matchedData.captured(1);
qInfo()<<data;
listVector[counter%numberItems]->push_back(data.toDouble());  //crashed in this line 
counter += 1;
}
}
//header type extraction
else if ((currentLine[0]=="#")) {
QRegularExpressionMatch match2 = headerFormat.match(currentLine);
domainType = match2.captured(1);
measureType = match2.captured(2);
measureMethod = match2.captured(3);
referenceImp = match2.captured(5);
}
}
qInfo()<<(listVector)[0]->size();
// extraction completed, close file
file.close();
}

我尝试使用的数据

! S3P File: Measurements: Mag<S11> Ang<S11> Mag<S12> Ang<S12> Mag<S13> Ang<S13>! row 1 
!             Mag<S21> Ang<S21> Mag<S22> Ang<S22> Mag<S23> Ang<S23>! row 2 
!             Mag<S31> Ang<S31> Mag<S32> Ang<S32> Mag<S33> Ang<S33>! row 3 

# Hz S  dB   R 50
10000000 -52.282864 83.754532 -15.67896 -0.49186572 -1.5670134 -1.301762
-15.748223 -0.10280456 -50.764286 149.86646 -56.900387 -70.843872
-1.5776229 -1.2955924 -71.374367 -141.70441 -48.866203 65.932648
29987500 -60.731289 51.972256 -15.724875 -1.8122723 -1.5746148 -3.2653806
-15.716247 -1.8535374 -52.617325 178.46486 -62.476551 5.2978568
-1.5735339 -3.2886419 -65.686714 36.462376 -61.792774 9.9133005
49975000 -64.386765 2.9596229 -15.755823 -3.1201222 -1.5706615 -5.4831181
-15.738674 -3.0600834 -52.237274 169.75545 -58.964714 27.526367
-1.5741955 -5.4729681 -58.479481 47.793446 -56.108696 63.561378
69962500 -60.896507 -36.51757 -15.757278 -4.3601637 -1.577867 -7.6754441
-15.747104 -4.3896055 -50.548729 177.66254 -57.043007 9.1148758
-1.5749472 -7.6169047 -56.500164 6.8388286 -57.866039 -28.409134
89950000 -55.328976 -45.191593 -15.76739 -5.6117945 -1.5797645 -9.8418713
-15.749889 -5.5769963 -50.809414 173.62177 -57.651119 -0.7305429
-1.5803487 -9.7842817 -56.621773 5.4209571 -66.583694 23.118269
109937500 -60.013988 -42.251778 -15.770485 -6.7288284 -1.5785085 -12.021818
-15.764176 -6.7430348 -51.211327 170.20062 -56.137653 -1.5785257
-1.5805914 -11.985991 -55.860668 0.92532599 -58.335781 15.83968
129925000 -55.2337 -47.264515 -15.773083 -7.9232988 -1.5834771 -14.20496
-15.74941 -7.9423704 -52.448433 166.58505 -57.46785 -5.3259425
-1.5812542 -14.161623 -57.962692 0.68304181 -66.230148 -5.397459
149912500 -52.218529 -63.591721 -15.763268 -9.1296005 -1.5826913 -16.37332
-15.770909 -9.0827551 -51.982231 165.33054 -56.151371 -14.161432
-1.586452 -16.33128 -56.938805 -20.056446 -57.895336 -49.978813
169900000 -48.723946 -68.877541 -15.772202 -10.29782 -1.58857 -18.537006
-15.756567 -10.253307 -53.100327 166.75829 -57.286308 -21.661839
-1.5859301 -18.455862 -58.19286 -23.011927 -56.403473 -61.177219
189887500 -47.055653 -63.976929 -15.761551 -11.486256 -1.5892707 -20.66897
-15.756705 -11.417432 -52.570255 161.31961 -58.11451 -14.260036
-1.589681 -20.645853 -58.313042 -19.368044 -56.06601 -59.461319
209875000 -48.199955 -65.147034 -15.758836 -12.678895 -1.5931528 -22.858885
-15.750708 -12.633892 -54.576355 159.43607 -57.476353 -23.090178
-1.5905892 -22.808754 -57.229259 -22.673452 -53.756973 -77.388786
229862500 -47.620575 -65.079559 -15.761273 -13.856562 -1.5960027 -24.995838
-15.757499 -13.857363 -54.944546 168.24443 -57.51046 -25.383596
-1.5932056 -24.973335 -56.755974 -24.434172 -53.588017 -78.209816
249850000 -45.54829 -72.373695 -15.759305 -15.106434 -1.5959594 -27.192209
-15.754061 -15.04305 -56.757584 167.29137 -57.757648 -35.331924
-1.593374 -27.133352 -57.15892 -38.918037 -52.251003 -78.6092
269837500 -45.209282 -79.048836 -15.762296 -16.186449 -1.595193 -29.34836
-15.761364 -16.268795 -56.31012 155.37877 -58.96965 -36.80077
-1.6012613 -29.305353 -58.892376 -30.59782 -51.139919 -88.653198
289825000 -44.495762 -75.946327 -15.77701 -17.442837 -1.5954289 -31.487225
-15.759547 -17.546104 -57.791164 136.05252 -58.239506 -44.324471
-1.6040369 -31.49972 -58.461231 -38.748329 -50.686073 -102.10234
309812500 -43.302803 -82.000542 -15.766918 -18.634033 -1.5996813 -33.661705
-15.761863 -18.641865 -61.730743 144.8166 -60.559978 -37.826778
-1.6024027 -33.65374 -57.697384 -42.566422 -51.518753 -105.38393
329800000 -43.696449 -78.707024 -15.772308 -19.829834 -1.6114002 -35.827797
-15.758872 -19.892492 -61.704247 131.07913 -60.35778 -39.423588
-1.6023126 -35.767818 -59.314335 -32.67252 -52.212837 -96.09478
349787500 -41.770233 -79.74221 -15.764846 -21.037899 -1.6063002 -37.965122
-15.761175 -21.141983 -67.287781 106.3756 -59.302685 -45.419971
-1.6093071 -37.931061 -58.065773 -47.054985 -50.113159 -100.45293
369775000 -41.834766 -85.074005 -15.755595 -22.26008 -1.6062098 -40.106232
-15.755444 -22.253096 -67.486336 95.830017 -60.84161 -46.56329
-1.6086895 -40.089161 -59.786545 -45.948189 -49.895309 -123.41199
389762500 -40.628685 -85.450836 -15.762123 -23.489182 -1.6093255 -42.268429
-15.763054 -23.396936 -68.248146 81.470787 -60.379967 -55.129562
-1.608591 -42.201767 -61.377071 -50.809517 -50.866241 -127.41563
409750000 -40.523582 -86.431625 -15.7611 -24.674004 -1.6127316 -44.40868
-15.759981 -24.647635 -75.545479 166.72386 -58.76609 -50.209095
-1.6096529 -44.381767 -58.312939 -53.500042 -50.608158 -125.55299
429737500 -39.49865 -87.925888 -15.768126 -25.870119 -1.6137398 -46.560898
-15.756283 -25.906502 -74.112823 -93.561935 -59.226101 -58.54623
-1.6217877 -46.567924 -59.321663 -53.369888 -48.844105 -125.06399

程序将运行并处理数据,直到

129925000 -55.2337 -47.264515 -15.773083 -7.9232988 -1.5834771 -14.20496
-15.74941 -7.9423704 -52.448433 166.58505 -57.46785 -5.3259425
-1.5812542 -14.161623 -57.962692 0.68304181 -66.230148 -5.397459

您有太多的双精度值,无法使用qint8类型作为计数器来读取所有双精度值。counter具有类型qint8,因此您可以保存有符号整数 在范围[-128,127]中,当您分配大于 127 ( 在您的文件中,双精度值多于 127( 对于您的counter变量,它被视为负数,并且 调用索引运算符时应用崩溃,因为counter%numberItems是负值。

listVector[counter%numberItems]->push_back(data.toDouble()); // accessing list item with negative index

counter更改为更宽的整数类型,例如 qint32 或 重置代码中某处的值counter以避免 整数值的溢出。

因此,首先,您分配了大量堆内存,而之后从未清除过它。没有相应删除的new语句中的每一个都会导致内存泄漏。

此外,如果nPorts.toInt()转换失败或解析不正确,您将获得等于奇怪事物的numberItems。然后,使用该数字来分配listVector假设它具有您期望的大小。然后使用下标运算符直接对其进行操作,而不检查对象的实际大小或用于分配它的数量。

使用这样的下标运算符访问动态分配的对象,而不检查实际大小,绝对会使程序崩溃。

我相信你的问题出在这条线上:(编辑:最初没有阅读这一行的评论,但很高兴你知道这一点。 这进一步加深了我的怀疑。

listVector[counter%numberItems]->push_back(data.toDouble()); //crashed in this line

这是因为您因超出 listVector 对象的边界而出现分段错误,这不是您期望的大小。我可能是错的,但至少有一些事情需要开始调查。