wxExcute Process Handling Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Ruro
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed May 23, 2007 2:20 pm
Location: Verona, Italy

wxExcute Process Handling

Post by Ruro »

Hi,

I'm try to handle the result of some python script at the end of the process.

The scripts help me in getting some data from different devices, so I launch multiple script at the same time.

I have a question about the wxProcess object created in the starting call of the wxExcute, here the code that I'm using:

Code: Select all


wxVector<MyDevice> ListDevices;

void DevicesManagerFrame::StartDeviceRequest(int sel)
{
    
    wxString cmd = _T("");
    cmd=ListDevices[sel].ReqManager.GetCurrentCmdStr();
    
    if(cmd==_T(""))
        return ;
        
    wxProcess *ObjProcess = new wxProcess(this,ListDevices[sel].creationID);

    ObjProcess->Bind(wxEVT_END_PROCESS ,&DevicesManagerFrame::HandlerEndDeviceRequest,this);
    
    ListDevices[sel].ReqManager.pidProcess = wxExecute(cmd,32,ObjProcess);

    ListDevices[sel].ReqManager.ObjProcess=NULL;
    ListDevices[sel].ReqManager.ObjProcess=ObjProcess;
    ListDevices[sel].ReqManager.SetCurrentCmdRunning();

    if ( ListDevices[sel].ReqManager.pidProcess == -1 )
    {
        //here I delete the pointer if something goes wrong
        ListDevices[sel].ReqManager.EndProccess(-1);

    }
    return ;

}

void DevicesManagerFrame::HandlerEndDeviceRequest(wxProcessEvent& event)
{
    int idEvent=event.GetId();
    
    for(unsigned int iDev=0;iDev<ListDevices.size();iDev++)
    {
        if(ListDevices[iDev].creationID==idEvent)
        {
            if(event.m_exitcode==0)
            {
                ListDevices[iDev].RefreshTimeoutCon();
            }else 
                //the script generate a file at the end, so I can have the result to be checked.
                //do I need to delete the Process pointer here or the event handle it?
                ListDevices[iDev].ReqManager.EndProccess(event.m_exitcode);
            return;
        }
    }
    return;

}

//the function that delete the pointer
void MyDeviceRequestManagerPy::EndProccess(int returnCode)
{
    if(returnCode==-1)
    {
        if(ObjProcess!=NULL)
            wxDELETE(ObjProcess);
    }   
    ObjProcess=NULL;
    
}
Do I need to delete everytime the pointer of wxProcess or if the process terminate correctly I don't have to?

Thanks for your advice,

Best Regards,

Nicola.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxExcute Process Handling

Post by PB »

Please read the note in wxProcess docs: https://docs.wxwidgets.org/stable/classwx_process.html

Deleting the wxProcess instance should not depend on the process exit code but on your handling the event.
Ruro
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed May 23, 2007 2:20 pm
Location: Verona, Italy

Re: wxExcute Process Handling

Post by Ruro »

HI PB,

thanks for the link to the doc, I was looking in the wrong spot.

I usually delete the wxProcess on the event, and that works fine.

I was wondering the need of the delete, because recently I added another class that use the same structure to handle the wxProcess, but if I delete the pointer, I receive a segmentation fault and the program crash.

I probably should look for the root of the problem elsewhere.

Thanks you for the advise.

Nicola.
Ruro
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed May 23, 2007 2:20 pm
Location: Verona, Italy

Re: wxExcute Process Handling

Post by Ruro »

Hi Again,

So the StartDeviceRequest(int sel) function is called periodically (about 2 min), and I was wonder if the repetition of this instruction is safe:

Code: Select all

ObjProcess->Bind(wxEVT_END_PROCESS ,&DevicesManagerFrame::HandlerEndDeviceRequest,this);
Do I need to Bind the Obj every time or the first time of the Bind is good for the future call?

I can't write the macro in the event table manualy, the list of the devices is not fixed, and using one method to manage multiple different device is something I would avoid.

Thanks again,

Nicola.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxExcute Process Handling

Post by PB »

I have recently experienced a crash when deleting wxProcess in a code handling wxEVT_END_PROCESS Bind()ed by a wxProcess instance. However, it was in a lambda and the issue disappeared when the handler was changed to be a method instead of lambda.

IIRC, it worked even when I changed "MyProcess->Bind()" to just "Bind()" in a a method of a class derived from wxFrame (where the wxFrame instance was passed to wxProcess one in the ctor).

Either way, I think that no harm should be done by calling "process->Bind()" multiple times but it may work even just calling "Bind()" once as described above.

BTW, I am not sure if the code below is correct, perhaps it should check the PID for 0 (or <=0) instead?
Ruro wrote: Tue May 30, 2023 7:35 am

Code: Select all

    ListDevices[sel].ReqManager.pidProcess = wxExecute(cmd,32,ObjProcess);

    ListDevices[sel].ReqManager.ObjProcess=NULL;
    ListDevices[sel].ReqManager.ObjProcess=ObjProcess;
    ListDevices[sel].ReqManager.SetCurrentCmdRunning();

    if ( ListDevices[sel].ReqManager.pidProcess == -1 )
    {
        //here I delete the pointer if something goes wrong
        ListDevices[sel].ReqManager.EndProccess(-1);

    }
(I am sure that hard-coded literal should NOT be used as flags passed to wxExecute() there).
Ruro
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed May 23, 2007 2:20 pm
Location: Verona, Italy

Re: wxExcute Process Handling

Post by Ruro »

Hi PB,

thanks for the explanation, I can try to use Bind() instead of the other one and check if the problem persist.

You are right about the error check, it should be <=0, and the flag is also a bad habit that has to be correct.

After the change, I will report the result here.

Best Regards,

Nicola.
Ruro
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed May 23, 2007 2:20 pm
Location: Verona, Italy

Re: wxExcute Process Handling

Post by Ruro »

Hi PB,

I probably found the error.

When I run the script, I have a timeout to check the script does not take to long to get completed.

When the script take to much time, I close the wxProcess, but the script probably still running and there comes the error.

I will fix the chain to avoid deleting the wxProcess when the script is still in running.

Thanks for helping me out.

Nicola.
Post Reply