ATLAS Offline Software
PerfMonTestManyLeaksAlg.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // PerfMonTestManyLeaksAlg.cxx
8 // Implementation file for class PerfMonTest::ManyLeaksAlg
9 // Author: R.Sesuter
11 
12 
13 // STL includes
14 
15 // FrameWork includes
16 #include "Gaudi/Property.h"
17 
18 // PerfMonTests includes
20 
21 using namespace PerfMonTest;
22 
23 long** ManyLeaksAlg::m_pointers ATLAS_THREAD_SAFE = NULL;
24 
26 // Public methods:
28 
29 // Constructors
31 ManyLeaksAlg::ManyLeaksAlg( const std::string& name,
32  ISvcLocator* pSvcLocator ) :
33  AthAlgorithm( name, pSvcLocator ),
34  m_stillReachable ( NULL ),
35  m_possibleLost ( NULL ),
36  m_indirectlyLost ( NULL ),
37  m_definitelyLost ( NULL )
38 {
39  //
40  // Property declaration
41  //
42  //declareProperty( "Property", m_nProperty );
43 
44  declareProperty( "LeakSize",
45  m_leakSize = 10,
46  "Number of longs to be leaked just once" );
47  declareProperty( "LeakInInit",
48  m_leakInInit = false,
49  "Where it will leak: initialize or execute(default)" );
50 }
51 
52 // Destructor
55 {
56  ATH_MSG_DEBUG ( "Calling destructor" ) ;
57 }
58 
59 // Athena Algorithm's Hooks
62 {
63  ATH_MSG_INFO ( "Initializing " << name() << "..." ) ;
64 
65  // in m_pointers, we'll store the pointers to the allocated space
66  // they need to exist beyond the lifetime of this algorithm
67  // or the'll be gone and everything is labelled as 'definitly lost'
68 
69  if ( !bool(m_pointers) )
70  m_pointers = new long* [4];
71 
72  if ( m_leakInInit ) leakAll();
73 
74  return StatusCode::SUCCESS;
75 }
76 
78 {
79  ATH_MSG_INFO ( "Finalizing " << name() << "..." ) ;
80 
81  return StatusCode::SUCCESS;
82 }
83 
85 {
86  ATH_MSG_DEBUG ( "Executing " << name() << "..." ) ;
87 
88  if ( 0 == m_leakSize ) {
89  return StatusCode::SUCCESS;
90  }
91 
92  if ( ! m_leakInInit ) leakAll();
93 
94  return StatusCode::SUCCESS;
95 }
96 
98 {
99  // 1024: if it's too small, they'll be classified as definitely lost
100  // check Scott's wiki - will be created on the heap ?
101  const unsigned int maxSize = 256;
102  long **array = new long* [maxSize];
103  for ( unsigned int i=0; i<maxSize; ++i )
104  array[i] = NULL;
105 
106  // this one's definitely lost
107  m_pointers[0] = this->definitelyLostFct(array);
108 
109  // this one's indirectly lost
110  m_pointers[2] = this->indirectlyLostFct(array);
111 
112  // this one's possible lost
113  m_pointers[1] = this->possibleLostFct(array);
114 
115  // we'll create a small 'mem leak', but it's still reachable
116  m_pointers[0] = this->stillReachableFct(array);
117 
118  // don't delete m_pointers
119 }
120 
121 long* ManyLeaksAlg::stillReachableFct( long** /* array */ )
122 {
123  if ( ! bool(m_stillReachable) )
124  {
125  m_stillReachable = new long[m_leakSize];
126 
127  // do something with it ...
128 
129  // we don't really need it any more, but we never delete m_stillReachable
130  // delete [] m_stillReachable;
131  }
132  return m_stillReachable;
133 }
134 
135 long* ManyLeaksAlg::possibleLostFct( long** /* array */ )
136 {
137  if ( ! bool(m_possibleLost) )
138  {
139  // this one's possible lost
140  m_possibleLost = new long[m_leakSize];
141  // move along
142  m_possibleLost++;
143  m_possibleLost++;
144  m_possibleLost++;
145  m_possibleLost++;
146 
147  // do something with it ...
148 
149  // we don't really need it any more, but we never delete m_possibleLost
150  // delete [] m_possibleLost (must point to original location !!!)
151  }
152  return m_possibleLost;
153 }
154 
156 {
157  if ( ! bool(m_indirectlyLost) )
158  {
159  // this one's indirectly lost
160  m_indirectlyLost = new long[m_leakSize];
161 
162  // move along
171 
172  // save pointer in array
173  array[127] = m_indirectlyLost;
174 
175  // do something with it ...
176 
177  // and (accidentially) invalidate pointer
178  m_indirectlyLost = NULL;
179 
180  // we don't need it any more, but we don't clean up and delete array
181  // array goes out of scope and it's lost !
182  // array still points to m_indirectlyLost which is now indirectly lost
183  }
184  return m_indirectlyLost;
185 }
186 
187 long* ManyLeaksAlg::definitelyLostFct( long** /* array */ )
188 {
189  if ( ! bool(m_definitelyLost) )
190  {
191  // this one's definitely lost
192  m_definitelyLost = new long[m_leakSize];
193 
194  // do something with it ...
195 
196  // and (accidentially) invalidate pointer
197  m_definitelyLost = NULL;
198  // now, we cannot delete m_possibleLost, because it's pointer value is gone
199  // there's no reference in memory, so we cannot even do :
200  // delete [] m_possibleLost;
201  }
202  return m_definitelyLost;
203 }
PerfMonTest::ManyLeaksAlg::indirectlyLostFct
long * indirectlyLostFct(long **array)
this one's indirectly lost
Definition: PerfMonTestManyLeaksAlg.cxx:155
PerfMonTest::ManyLeaksAlg::stillReachableFct
long * stillReachableFct(long **array)
Definition: PerfMonTestManyLeaksAlg.cxx:121
PerfMonTest::ManyLeaksAlg::m_stillReachable
long * m_stillReachable
Definition: PerfMonTestManyLeaksAlg.h:80
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
ATLAS_THREAD_SAFE
long **ManyLeaksAlg::m_pointers ATLAS_THREAD_SAFE
Definition: PerfMonTestManyLeaksAlg.cxx:23
PerfMonTest::ManyLeaksAlg::possibleLostFct
long * possibleLostFct(long **array)
this one's possible lost
Definition: PerfMonTestManyLeaksAlg.cxx:135
PerfMonTest::ManyLeaksAlg::~ManyLeaksAlg
virtual ~ManyLeaksAlg()
Destructor:
Definition: PerfMonTestManyLeaksAlg.cxx:54
PerfMonTest::ManyLeaksAlg::m_definitelyLost
long * m_definitelyLost
Definition: PerfMonTestManyLeaksAlg.h:89
PerfMonTest::ManyLeaksAlg::m_leakInInit
bool m_leakInInit
Property to setup the location of the leak, in initialize (true) or execute (false)
Definition: PerfMonTestManyLeaksAlg.h:75
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
PerfMonTest::ManyLeaksAlg::m_possibleLost
long * m_possibleLost
Definition: PerfMonTestManyLeaksAlg.h:83
PerfMonTest
PerfMonTestPolyVectorAlg.h Example for the memory optimization tutorial.
Definition: Control/PerformanceMonitoring/PerfMonTests/src/Hit.h:7
PerfMonTest::ManyLeaksAlg::m_indirectlyLost
long * m_indirectlyLost
Definition: PerfMonTestManyLeaksAlg.h:86
PerfMonTest::ManyLeaksAlg::m_leakSize
int m_leakSize
Property to setup the size of the leak.
Definition: PerfMonTestManyLeaksAlg.h:72
AthAlgorithm
Definition: AthAlgorithm.h:47
PerfMonTest::ManyLeaksAlg::initialize
virtual StatusCode initialize()
Definition: PerfMonTestManyLeaksAlg.cxx:61
lumiFormat.array
array
Definition: lumiFormat.py:91
PerfMonTest::ManyLeaksAlg::ManyLeaksAlg
ManyLeaksAlg()
Default constructor:
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
PerfMonTest::ManyLeaksAlg::leakAll
void leakAll()
this one's definitely lost
Definition: PerfMonTestManyLeaksAlg.cxx:97
PerfMonTest::ManyLeaksAlg::finalize
virtual StatusCode finalize()
Definition: PerfMonTestManyLeaksAlg.cxx:77
PerfMonTest::ManyLeaksAlg::definitelyLostFct
long * definitelyLostFct(long **array)
this one's definitely lost
Definition: PerfMonTestManyLeaksAlg.cxx:187
PerfMonTest::ManyLeaksAlg::execute
virtual StatusCode execute()
Definition: PerfMonTestManyLeaksAlg.cxx:84
PerfMonTestManyLeaksAlg.h