| Summary: | Invalid code in G4QGSParticipants.cc - crash on Windows | ||
|---|---|---|---|
| Product: | Geant4 | Reporter: | Tom Roberts <tjrob> |
| Component: | processes/hadronic/models/parton_string/qgsm | Assignee: | Gunter.Folger |
| Status: | RESOLVED FIXED | ||
| Severity: | major | ||
| Priority: | P5 | ||
| Version: | 9.1 | ||
| Hardware: | All | ||
| OS: | All | ||
Fixed in geant4 9.2 |
The file G4QGSParticipants.cc has an invalid loop, which happens to work on Linux and Mac OS X, but crashes on Windows. This has been present since at least Geant4 9.0. void G4QGSParticipants::PerformSoftCollisions() { std::vector<G4InteractionContent*>::iterator i; for(i = theInteractions.begin(); i != theInteractions.end(); i++) { G4InteractionContent* anIniteraction = *i; G4PartonPair * aPair = NULL; if (anIniteraction->GetNumberOfSoftCollisions()) { // ... code which does not use i ... delete *i; i=theInteractions.erase(i); i--; } } } The problem comes when i refers to the first element of the vector. It is not valid to decrement an iterator before the first element of the vector. This crashes on Windows. Here is a workaround (flagged by //TJR): void G4QGSParticipants::PerformSoftCollisions() { std::vector<G4InteractionContent*>::iterator i; //TJR start_over: for(i = theInteractions.begin(); i != theInteractions.end(); i++) { G4InteractionContent* anIniteraction = *i; G4PartonPair * aPair = NULL; if (anIniteraction->GetNumberOfSoftCollisions()) { //// code which does not use i delete *i; i=theInteractions.erase(i); //TJR if(i == theInteractions.begin()) goto start_over; i--; } } } A black-box analysis makes me think this workaround gives the same result as the original code, but an expert on this routine should re-write it to avoid the invalid operation (and the goto :-). An expert should also verify that it is intended that this function delete entries from theInteractions. Before the workaround, with 120 GeV protons on Cu using either QGSP or QGSP_BIC, my program crashed about every 5-10 events on Windows. After the workaround 20,000 events run without crashing on all three OSs; I cannot verify the correctness of the results, however.