I want to plot some data using matplotlib and a python script if a button in a Qt application is pressed. The slot which receives the signal from the startPythonButton is defined in the following way:
void MainWindow::on_wdgt_startPython_clicked()
{
    FILE* file;
    Py_Initialize();
    file=fopen("/home/user/test.py","r");
    if(file<0){
        Py_Finalize();
        return;
    }
    PyRun_SimpleFile(file,"/home/user/test.py");
    Py_Finalize();
    fclose(file);
}
The python script is defined as:
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,2*np.pi,100)
y=np.sin(x)
plt.plot(x,y)
plt.show()
When I run the Qt program and click the startPythonButton, the plot is shown normally. When I close the plot-window and click the startPythonButton again however, the Qt process receives a SIGSEGV when trying to call PyRun_SimpleFile.
Any ideas where the error could be?
So it turns out that the second fopen() call does not fail (e.g. does not return NULL):
void MainWindow::on_wdgt_startPython_clicked()
{
    FILE* file;
    Py_Initialize();
    file=fopen("/home/user/test.py","r");
    if(file==NULL){
        std::stderr << "Failed to open file." << std::endl;
        Py_Finalize();
        return;
    }
    PyRun_SimpleFile(file,"/home/user/test.py");
    Py_Finalize();
    if(fclose(file)!=0){
        std::stderr << "Failed to close file." << std::endl;
        return;
    };
}
