Dynamically creating arrays at the construct function of EventAction

Hi,
Segementation faults happened when I run optical photon simulations of SPECT detectors. For convience, I used user defined commands to control the the number of pixels of the detector and new an int array in the construct function of EventAction class to store the number of absorbed photon of each pixel like:

// in .hh private variable
G4int* Signals;
\\\
// in .cc construct function
Signals = new int[num] // num is the number user set through user-defined commands.

However, segementation faults happened once I ran the simulation in multithread mode. I have used the gdb tool to find where the fault happended, unluckily, its position changed all the time and was not located in my code. At one time it shows a pointer was deleted two times.

Thus, I thought it seemed like memory leaks and checked the pointer in my codes. Finally, I changed the way I allocate memory for this variable:

G4int Signals[1600];

the simulation finally worked well.

The strange phenomenon makes me confused about the multi-thread in Geant4. Should one complete EventAction belong to a certain thread? Or even one event can be processed by different thread? Anyway, I would like to figure out how to dynamically allocate memory in EventAction correctly.

Thanks!

_Geant4 Version:_geant4-11-02-patch-02 [MT]
_Operating System:_Ubuntu 20.04.6 LTS
_Compiler/Version:_Gcc 9.4.0
_CMake Version:_Cmake 3.16.3

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.