Bug Summary

File:scratch/gluex/scan-build-work/hdgeant4/src/G4fixes/G4FieldManagerStore.cc
Location:line 68, column 22
Description:Attempt to free released memory

Annotated Source Code

1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27// $Id: G4FieldManagerStore.cc 69589 2013-05-08 14:35:28Z gcosmo $
28//
29// G4FieldManagerStore
30//
31// Implementation for singleton container
32//
33// History:
34// 07.12.07 J.Apostolakis Adapted from G4LogicalVolumeStore
35// --------------------------------------------------------------------
36
37#include "G4Types.hh"
38#include "G4FieldManagerStore.hh"
39#include "G4ChordFinder.hh"
40#include "G4MagIntegratorDriver.hh"
41#include "G4MagHelicalStepper.hh"
42
43// ***************************************************************************
44// Static class variables
45// ***************************************************************************
46//
47G4ThreadLocalthread_local G4FieldManagerStore* G4FieldManagerStore::fgInstance = 0;
48G4ThreadLocalthread_local G4bool G4FieldManagerStore::locked = false;
49
50// ***************************************************************************
51// Protected constructor: Construct underlying container with
52// initial size of 100 entries
53// ***************************************************************************
54//
55G4FieldManagerStore::G4FieldManagerStore()
56 : std::vector<G4FieldManager*>()
57{
58 reserve(100);
59}
60
61// ***************************************************************************
62// Destructor
63// ***************************************************************************
64//
65G4FieldManagerStore::~G4FieldManagerStore()
66{
67 Clean();
1
Calling 'G4FieldManagerStore::Clean'
7
Returning from 'G4FieldManagerStore::Clean'
68 if (fgInstance) { delete fgInstance; fgInstance=0; }
8
Taking true branch
9
Calling '~G4FieldManagerStore'
10
Taking true branch
11
Memory is released
12
Returning from '~G4FieldManagerStore'
13
Attempt to free released memory
69}
70
71// ***************************************************************************
72// Delete all elements from the store
73// ***************************************************************************
74//
75void G4FieldManagerStore::Clean()
76{
77 // Locks store for deletion of field managers. De-registration will be
78 // performed at this stage. G4FieldManagers will not de-register themselves.
79 //
80 locked = true;
81
82 size_t i=0;
83 G4FieldManagerStore* store = GetInstance();
2
Calling 'G4FieldManagerStore::GetInstance'
5
Returning from 'G4FieldManagerStore::GetInstance'
84
85 for(iterator pos=store->begin(); pos!=store->end(); pos++)
6
Loop condition is false. Execution continues on line 98
86 {
87 if (*pos) { delete *pos; }
88 i++;
89 }
90
91#ifdef G4GEOMETRY_DEBUG
92 if (store->size() < i-1)
93 { G4cout(*G4cout_p) << "No field managers deleted. Already deleted by user ?" << G4endlstd::endl; }
94 else
95 { G4cout(*G4cout_p) << i-1 << " field managers deleted !" << G4endlstd::endl; }
96#endif
97
98 locked = false;
99 store->clear();
100}
101
102// ***************************************************************************
103// Add field manager to container
104// ***************************************************************************
105//
106void G4FieldManagerStore::Register(G4FieldManager* pFieldManager)
107{
108 GetInstance()->push_back(pFieldManager);
109}
110
111// ***************************************************************************
112// Remove volume from container
113// ***************************************************************************
114//
115void G4FieldManagerStore::DeRegister(G4FieldManager* pFieldMgr)
116{
117 if (!locked) // Do not de-register if locked !
118 {
119 for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
120 {
121 if (*i==pFieldMgr) // For LogVol was **i == *pLogVolume ... Reason?
122 {
123 GetInstance()->erase(i);
124 break;
125 }
126 }
127 }
128}
129
130// ***************************************************************************
131// Return ptr to Store, setting if necessary
132// ***************************************************************************
133//
134G4FieldManagerStore* G4FieldManagerStore::GetInstance()
135{
136 if (!fgInstance)
3
Assuming 'fgInstance' is non-null
4
Taking false branch
137 {
138 fgInstance = new G4FieldManagerStore;
139 }
140 return fgInstance;
141}
142
143// ***************************************************************************
144// Globally reset the state
145// ***************************************************************************
146//
147void
148G4FieldManagerStore::ClearAllChordFindersState()
149{
150 G4ChordFinder *pChordFnd;
151
152 for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
153 {
154 pChordFnd = (*i)->GetChordFinder();
155 if( pChordFnd )
156 {
157 pChordFnd->ResetStepEstimate();
158 pChordFnd->SetFractions_Last_Next(-1, -1);
159 G4MagInt_Driver *driver =
160 dynamic_cast<G4MagInt_Driver*>(pChordFnd->GetIntegrationDriver());
161 if (driver) {
162 G4MagHelicalStepper *stepper =
163 dynamic_cast<G4MagHelicalStepper*>(driver->GetStepper());
164 if (stepper)
165 stepper->ResetState();
166 }
167 }
168 }
169}