Problem 2350 - G4strstreambuf: bug in destructor
Summary: G4strstreambuf: bug in destructor
Status: RESOLVED FIXED
Alias: None
Product: Geant4
Classification: Unclassified
Component: processes/hadronic/models/de_excitation (show other problems)
Version: 10.7
Hardware: All All
: P4 minor
Assignee: Vladimir.Ivantchenko
URL:
Depends on:
Blocks:
 
Reported: 2021-03-26 10:34 CET by Andre
Modified: 2021-04-20 10:18 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 Andre 2021-03-26 10:34:08 CET
In the execution of Geant4 Simulation via DD4hep, random output was printed at the end of the execution from the destructor of the G4strstreambuffer.
More details https://github.com/AIDASoft/DD4hep/issues/798.

This bug is triggered because we don't use G4cout and G4endl anywhere in our execution, and because the G4DeexPrecoParameters::StreamInfo function never uses G4endl https://gitlab.cern.ch/geant4/geant4/-/blob/master/source/processes/hadronic/models/de_excitation/management/src/G4DeexPrecoParameters.cc#L294

to flush the output to screen. This lack of G4end is maybe also a bug, because this G4DeexPrecoParameter output appears far from where it should have been printed.

But because G4endl isn't used, the G4strstreambuf sync() is never called, so at the end when the destructor is called and char* buffer is printed this buffer isn't '\0' terminated leading to the printout of random data.

Proposed changes:

1) Add 

buffer[count] = '\0';

to ~G4strstreambuf

```
  if(count != 0) {
    buffer[count] = '\0';
    std::cout << buffer;
  }
```

2) Add G4endl to G4DeexPrecoParameters::StreamInfo
and similar functions.
Comment 1 Vladimir.Ivantchenko 2021-04-13 17:14:56 CEST
Andre,

I have replaced at the end of printout "\n" by G4endl. In my test there is no difference - the line break is the same. Can you, confirm, that this fix is working for you? 

Vladimir
Comment 2 Andre 2021-04-13 18:09:22 CEST
Hi Vladimir,

The main difference between `G4endl`(or std::endl); and `"\n"` is that the first one also causes a call to `flush()` (or equivalent), right?


The change in https://github.com/AIDASoft/DD4hep/pull/799/files
```
  try  {
    return Geant4Exec::run(*this);
    auto result = Geant4Exec::run(*this);
    // flush the geant4 stream buffer
    G4cout << G4endl;
    return result;
  }
```
really fixes the "random" printout (also caused by that missing '\0', I believe). Having the G4endl at the end of G4DeexPrecoParameters::StreamInfo should also make this output appear at the actual time it is called and not at the end of our execution.

Cheers,
Andre
Comment 3 Vladimir.Ivantchenko 2021-04-20 10:18:00 CEST
Hello,

Many thanks for this report.

The problem is fixed and will be available in next public version of Geant4 and in the next patch to 10.7

Vladimir