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.
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 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.
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.
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?