Problem 1018 - Invalid code in G4QGSParticipants.cc - crash on Windows
Summary: Invalid code in G4QGSParticipants.cc - crash on Windows
Status: RESOLVED FIXED
Alias: None
Product: Geant4
Classification: Unclassified
Component: processes/hadronic/models/parton_string/qgsm (show other problems)
Version: 9.1
Hardware: All All
: P5 major
Assignee: Gunter.Folger
URL:
Depends on:
Blocks:
 
Reported: 2008-08-08 00:33 CEST by Tom Roberts
Modified: 2009-05-19 14:26 CEST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this problem.
Description Tom Roberts 2008-08-08 00:33:26 CEST
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.
Comment 1 Gunter.Folger 2009-05-19 14:26:50 CEST
Fixed in geant4 9.2