PDA

View Full Version : QT crashed during running



sh
24th June 2018, 22:06
hi all,
I've trying to load some data from file into a nested list. However, the application crashed during loading the data. I've put in some debug line in the code, and found the code crashed in appending data in my inner most list(line #76). Can someone please help me to understand what happened and how to fix it? Thanks.

main.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 <QVector>
#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>*>;

for(auto i=0; i<numberItems; i++){
QList<double> *list = new QList<double>;
(*listVector)[i] = list;
}

//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);
QList<double> *currentList= (*listVector)[counter%numberItems];
currentList->append(data.toDouble());
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();
}


data:


! 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

d_stranz
24th June 2018, 23:42
What do you think happens when your code hits line 45? All you have done in line 41 is to allocate a -pointer-. You haven't given the QList that pointer points to any size. So if you have a zero-sized QList, and you try to assign something to the i-th member of it, what happens?

sh
25th June 2018, 04:13
What do you think happens when your code hits line 45? All you have done in line 41 is to allocate a -pointer-. You haven't given the QList that pointer points to any size. So if you have a zero-sized QList, and you try to assign something to the i-th member of it, what happens?

Thanks for helping, I've tried to allocate the size of
listVector using the value from
numberItems at line 41, then initiate each nested list with pointers(line 44) that has many items(say 1000), however, the program still crashed.....

d_stranz
25th June 2018, 05:14
If you make listVector a QList (not QList *) member variable of your SNP_Plotter class, then you can use QList::push_back() to add new elements onto the the end of the list.

Your logical error is in thinking that when you allocate a pointer, that also allocates a size for whatever list that is. It doesn't. All it does is allocate a pointer to a list. There is no list, just a pointer to a list. If you want to put things on the list, then you have to use list->push_back() or a similar method. And if what you have allocated is a pointer to a list of pointers to lists, then you have to make sure that what you are pushing onto the main list is pointers to lists that have been allocated with new() and not pointers to things you have created on the stack (e.g. list->push_back( &stackList); ). That will cause a crash too, because those stack things will be deleted as soon as the method that creates them exits and your stack will contain a bunch of pointers to deleted objects.