I want to pass G-CODEs to a 3D printer from my Qt app. I have a function that takes a QString and passes it to an external program as an argument:
void MainWindow::gcodeRelay( const QString &gcode )
{
QObject *parent;
relayProcess= new QProcess(parent);
QString program = "./gcoderelay/gcoderelay";
relayProcess->start(program, QStringList() << ui->tcp_server->text() << ui->client_port->text() << gcode );
}
But calling the function causes the program to crash with segfault:
void MainWindow::on_custom_gcode_clicked()
{
QString gcode = ui->line_custom_gcode->text(); //SEGFAULT
gcodeRelay( gcode );
}
This works:
void MainWindow::on_custom_gcode_clicked()
{
QString gcode = "G28"; //WORKS
gcodeRelay( gcode );
}
Interestingly, this does not:
void MainWindow::on_custom_gcode_clicked()
{
QString gcode;
gcode = "G28"; //SEGFAULT
gcodeRelay( gcode );
}
According to debugger the crash occurrs when the new process is created (before it's started)
Why does this happen? Also I'm confused as to what is the difference between the last two snippets of code and how could it cause such a problem?
Thank you and I'm just learning so be kind :)