Unsolved Crash when using QProcess
-
i'm sorry if i start a topic wich is allready on this forum, but i couldn't find it.
Here is my simple code:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QProcess> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QObject *parent; QString program = "./path/to/Qt/examples/widgets/analogclock"; QStringList arguments; arguments << "-style" << "motif"; QProcess *myProcess = new QProcess(parent); myProcess->start(program, arguments); }
As you can see, i just trying the code wich is provided by the class explanation on
http://doc.qt.io/qt-5/qprocess.htmlThen i press the button and the program crashed.
I get this output:
../untitled1/mainwindow.cpp: In member function 'void MainWindow::on_pushButton_clicked()':
../untitled1/mainwindow.cpp:24:46: warning: 'parent' may be used uninitialized in this function [-Wmaybe-uninitialized]
QProcess *myProcess = new QProcess(parent);As you can see, i don't get an error, but it stops unexpectedly.
Does somebody knows, why this is hapening?
[edit koahnig: added code tags]
-
Hi and welcome to devnet
void MainWindow::on_pushButton_clicked() { QObject *parent = 0; // <<<<< change here QString program = "./path/to/Qt/examples/widgets/analogclock"; QStringList arguments; arguments << "-style" << "motif"; QProcess *myProcess = new QProcess(parent); myProcess->start(program, arguments); }
You are suing the code of the example, but there should be some statements inbetween for initialization of parent. Those are missing and therefore the error message.
You could also do
QProcess *myProcess = new QProcess();
without using the pointer respectively using its default value. This would have the same effect.
Note this will leave you with a memory leak. Each time you start the routine a new object will be created.
The source from documentation is simply to show how to handle, but it is IMHO not really good.
-
@koahnig thx so much for replying. The program works after i change the initialization of parents. I mean, there is no error or crash. But the external programs doesn't start. After i click on the pushButton , nothing happens. I change the code like this and test a txt data and still no response.
void MainWindow::on_pushButton_clicked()
{
QObject *parent=0;
QString program ="C:\Users\Alex\Desktop\test.txt";
// QString program = "./path/to/Qt/examples/widgets/analogclock";
QStringList arguments;
// arguments << "-style" << "motif";QProcess *myProcess = new QProcess(parent); myProcess->start(program);
what is the problem now ?
-
QString program ="C:\Users\Alex\Desktop\test.txt";
That is not an executable program. Typically you have to have an extension ".exe" on windows. That is the extension of a text file. The system will try to start that.
Try to create a hello world program which you can start using QProcess.
-
I build a c++ Helloworld program.There‘s still no output.
After i search online, i get three different ways to open external programs. And i tried them one by one.
QObject *parent=0; QString program="C:/Users/Alex/Documents/example/hello/Debug/hello.exe"; system("C:/Users/Alex/Documents/example/hello/Debug/hello.exe");// first QProcess::execute(program);//second QProcess *myProcess = new QProcess(parent); myProcess->start(program);//third
By the first one , a cmd window comes out and closes very quickly, no output in Application output.
By the second one , Helloworld in Application output.
By the third one, nothing happened.Then i tried the second one to open the another program which i will used in my project.
The code of the program is here:
#include <iostream> #include <fstream> #include <windows.h> using namespace std; int main() { int cnt=0; int a[20]; ifstream fin("C:\\Users\\Alex\\Desktop\\test.txt", ios::in); if(!fin){ printf("The file is not exist!"); return -1; } for (;cnt<=4; ) { fin >> a[cnt]; cout<<cnt <<"\n"<<a[cnt]<<"\n"; cnt++; } fin.close(); // MessageBox(NULL,TEXT("success"),TEXT("success"),0); return 0; }
The output in the c++ is
0
0
1
1
2
2
3
3
4
4But the output in QT is
0
0
1
1
2
2
3
3
4
4
5
-892008133
6
0
7
0
8
0
9
4201152
10
2686704
11
2686760
12
2686916
.
.
.
.
.
3914
0
3915
0
3916
0
3917
0then crash.
Is it problem with qt or with my c++ program?
-
What do you mean with output in Qt?
QObject *parent=0; QString program="C:/Users/Alex/Documents/example/hello/Debug/hello.exe"; system("C:/Users/Alex/Documents/example/hello/Debug/hello.exe");// first
The system call will just open a command line, execute your problem and possibly crash also. Because of speed it is hard to see anything for checking.
QProcess::execute(program);//second
That is a static routine of QProcess. It is basically similar to your system call, but it redircts the output to Application output.
QProcess *myProcess = new QProcess(parent); myProcess->start(program);//third
This one is creating the object and starting it, but your application does not wait and continues with processing. The output is written to the QProcess object and left there.
You can use waitForFinished for your process to finish. Afterwards you can readAllStandardOutput and write it wherever you like.
All this is possible, but I would suggest that you are starting with more basic stuff from the tutorials for your own sake of learning. Also signals and slots handling will be helpful later on.
Concerning the crash you are mentioning, I think it is coming from your short hello world app.
-
The output in the c++ is
0
0
1
1
2
2
3
3
4
4But the output in QT is
0
0
1
1
2
2
3
3
4
4
5
-892008133
6
0
7
0
8
0
9
4201152
10
2686704
11
2686760
12
2686916
.
.
.
.
.
3914
0
3915
0
3916
0
3917
0then crash.
Is it problem with qt or with my c++ program?
How are you getting and displaying the output from the "Qt one"? To me it looks like you are referencing uninitialized memory.
You allocate an array of 21 integers
int a[20]
but you only use 5 of them. Which is fine.. overkill but fine. The Qt output one looks like it is showing the whole array of 21, then crashing as it indexes past the memory limit. So when it hits and reference a[21] you can crash at that point or any point after that. Usually, it would crash at the first reference, but in release mode it may let you get a bit further before crashing.Edit: you're also assuming that your file only has integer values in it. Using
fin
to read only into anint
from a text file is kind of asking for trouble. You're better off reading lines (assuming you separate ints by lines) and then converting the string to int and storing that. That would be much safer. Your crash here could also be coming from the data in the text file.