Problem 2138

Summary: G4FTFPModel has a memory leak
Product: Geant4 Reporter: Laurie Nevay <laurie.nevay>
Component: processes/hadronic/models/parton_string/diffractionAssignee: Alberto.Ribon
Status: RESOLVED FIXED    
Severity: major CC: Alberto.Ribon
Priority: P4    
Version: 10.5   
Hardware: Apple   
OS: Mac OS X   

Description Laurie Nevay 2019-02-18 16:20:07 CET
I'm experiencing a memory leak in my application that uses Geant4 that is noticeable and problematic for large (~250k) numbers of events.  I'm running protons in targets with no secondaries to look at diffractive scattering.

Profiling using Xcode Instrument's leak tool, I find a leak in G4FPFModel in GetStrings().  Indeed reading the code, you can see one.

Line 290 - theStrings = new G4ExcitedStringVector
Line 327 - theStrings = BuildStrings()

BuildStrings always allocates a new G4ExcitedStringVector. The pointer is returned from this function and overwrites the previous pointer. Therefore, we now have no pointer to the original object and it's leaked.

A simple fix is to put delete theStrings just before BuildStrings, however, I presume the initial allocation is so that we always return a valid object from this function. There are a few ways to remedy this that are clearer.  You could pass the pointer to BuildStrings() becomes BuildStrings(theStrings).

The leak is relatively small, but it builds up.  In our simulations we see around 0.8Mb/s with secondaries killed.

I think this is also in Geant4.10.4.p02.

Hopefully I've identified it correctly and this helps.

Best,
Laurie
Comment 1 Gunter.Folger 2019-02-19 15:57:46 CET
This was introduced in 10.5, in releases before theStrings was initialised to 0.
Its a local variable not passed to any method, just being re-assigned by BuildsStrings().
Need to check with the author why the initial new ... was added.
Comment 2 Alberto.Ribon 2019-04-16 12:26:51 CEST
The leak has been fixed in the coming patch G4 10.5.p01 (expected to be released this week), and in the development (i.e. for the future G4 10.6 release).
Thanks for the report.