Memory leak in SteppingAction in mutithread mode

Dear all,

I am trying to build an application to simulate a scintillation detector by a neutron source.
However, there seems to be a memory leak, by looking at PC’s memory usage slowly rising.

So I ran valgrind to verify the problem, and other than most of the message, I saw:

==10375== at 0x8D894F5: __printf_fp_l (printf_fp.c:673)
==10375== by 0x8D85A06: vfprintf (vfprintf.c:1642)
==10375== by 0x8DB190F: vsnprintf (vsnprintf.c:114)
==10375== by 0x8861CBB: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==10375== by 0x88909D6: std::ostreambuf_iterator<char, std::char_traits > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits > >::_M_insert_float(std::ostreambuf_iterator<char, std::char_traits >, std::ios_base&, char, char, double) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==10375== by 0x889CB8B: std::ostream& std::ostream::_M_insert(double) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==10375== by 0x72A51F4: tools::wcsv::ntuple::column::add() (in /home/nuplex/G4/install/lib/libG4analysis.so)
==10375== by 0x72A9681: G4TNtupleManagertools::wcsv::ntuple::AddNtupleRow(int) (in /home/nuplex/G4/install/lib/libG4analysis.so)
==10375== by 0x11B862: G4VAnalysisManager::AddNtupleRow() (G4VAnalysisManager.icc:111)
==10375== by 0x11B395: PracticeSteppingAction::UserSteppingAction(G4Step const*) (PracticeSteppingAction.cc:113)
==10375== by 0x6F2AD64: G4SteppingManager::Stepping() (in /home/nuplex/G4/install/lib/libG4tracking.so)
==10375== by 0x6F36131: G4TrackingManager::ProcessOneTrack(G4Track*) (in /home/nuplex/G4/install/lib/libG4tracking.so)
==10375== Uninitialised value was created by a stack allocation
==10375== at 0x11AFA7: PracticeSteppingAction::UserSteppingAction(G4Step const*) (PracticeSteppingAction.cc:36)

it says uninitialized value was created by a stack allocation.

I collect my output result from Steppingaction::UserSteppingAction(such as edep) and use FillNtupleDColumn right after that to fill my output file (separated by each thread). Maybe I am doing something wrong here?

I appreciate much for any insights. Thanks.

Hello,

the trace indicate a problem in lines 36 and 113 of PracticeSteppingAction.cc
You may also consult Geant4 extended examples where some tips how to make scoring in the MT mode are demonstarted.

VI

The warning about the uninitialized variable (very likely) has nothing to do with the memory leak. That just means something like:

double something;

instead of:

double something = 0.0;

A valgrind was probably concerned bc it was used somewhere bc an uninitialized variable will assume the “value” of whatever junk is non-zeroed at that location in memory so you might end up with something equal to 7.12345e-308. The “stack” is just memory term.

Given your description of storing something for every step and since your memory is “slowly” rising, it is entirely possible this is not a memory leak and you are just storing way more data than you realize by using the tuple.

Unless you need the energy deposit for every single step (a lot will be zero), I would suggest considering doing something like keeping a running sum of the energy deposit per-track or per-event and then just doing one insert. Add a print statement to your stepping action with a counter. It will be pretty clear very quickly how many times UserSteppingAction gets called.