ATLAS Offline Software
Loading...
Searching...
No Matches
PerfMonTestManyLeaksAlg.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2025 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
21using namespace PerfMonTest;
22
23long** ManyLeaksAlg::m_pointers ATLAS_THREAD_SAFE = NULL;
24
25
27{
28 ATH_MSG_INFO ( "Initializing " << name() << "..." ) ;
29
30 // in m_pointers, we'll store the pointers to the allocated space
31 // they need to exist beyond the lifetime of this algorithm
32 // or the'll be gone and everything is labelled as 'definitly lost'
33
34 if ( !bool(m_pointers) )
35 m_pointers = new long* [4];
36
37 if ( m_leakInInit ) leakAll();
38
39 return StatusCode::SUCCESS;
40}
41
43{
44 ATH_MSG_DEBUG ( "Executing " << name() << "..." ) ;
45
46 if ( 0 == m_leakSize ) {
47 return StatusCode::SUCCESS;
48 }
49
50 if ( ! m_leakInInit ) leakAll();
51
52 return StatusCode::SUCCESS;
53}
54
56{
57 // 1024: if it's too small, they'll be classified as definitely lost
58 // check Scott's wiki - will be created on the heap ?
59 const unsigned int maxSize = 256;
60 long **array = new long* [maxSize];
61 for ( unsigned int i=0; i<maxSize; ++i )
62 array[i] = NULL;
63
64 // this one's definitely lost
65 m_pointers[0] = this->definitelyLostFct(array);
66
67 // this one's indirectly lost
68 m_pointers[2] = this->indirectlyLostFct(array);
69
70 // this one's possible lost
71 m_pointers[1] = this->possibleLostFct(array);
72
73 // we'll create a small 'mem leak', but it's still reachable
74 m_pointers[0] = this->stillReachableFct(array);
75
76 // don't delete m_pointers
77}
78
79long* ManyLeaksAlg::stillReachableFct( long** /* array */ )
80{
81 if ( ! bool(m_stillReachable) )
82 {
83 m_stillReachable = new long[m_leakSize];
84
85 // do something with it ...
86
87 // we don't really need it any more, but we never delete m_stillReachable
88 // delete [] m_stillReachable;
89 }
90 return m_stillReachable;
91}
92
93long* ManyLeaksAlg::possibleLostFct( long** /* array */ )
94{
95 if ( ! bool(m_possibleLost) )
96 {
97 // this one's possible lost
98 m_possibleLost = new long[m_leakSize];
99 // move along
104
105 // do something with it ...
106
107 // we don't really need it any more, but we never delete m_possibleLost
108 // delete [] m_possibleLost (must point to original location !!!)
109 }
110 return m_possibleLost;
111}
112
114{
115 if ( ! bool(m_indirectlyLost) )
116 {
117 // this one's indirectly lost
118 m_indirectlyLost = new long[m_leakSize];
119
120 // move along
129
130 // save pointer in array
131 array[127] = m_indirectlyLost;
132
133 // do something with it ...
134
135 // and (accidentially) invalidate pointer
136 m_indirectlyLost = NULL;
137
138 // we don't need it any more, but we don't clean up and delete array
139 // array goes out of scope and it's lost !
140 // array still points to m_indirectlyLost which is now indirectly lost
141 }
142 return m_indirectlyLost;
143}
144
145long* ManyLeaksAlg::definitelyLostFct( long** /* array */ )
146{
147 if ( ! bool(m_definitelyLost) )
148 {
149 // this one's definitely lost
150 m_definitelyLost = new long[m_leakSize];
151
152 // do something with it ...
153
154 // and (accidentially) invalidate pointer
155 m_definitelyLost = NULL;
156 // now, we cannot delete m_possibleLost, because it's pointer value is gone
157 // there's no reference in memory, so we cannot even do :
158 // delete [] m_possibleLost;
159 }
160 return m_definitelyLost;
161}
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
#define ATLAS_THREAD_SAFE
void leakAll()
this one's definitely lost
Gaudi::Property< int > m_leakSize
Property to setup the size of the leak.
long * possibleLostFct(long **array)
this one's possible lost
Gaudi::Property< bool > m_leakInInit
Property to setup the location of the leak, in initialize (true) or execute (false)
long * indirectlyLostFct(long **array)
this one's indirectly lost
long * definitelyLostFct(long **array)
this one's definitely lost
virtual StatusCode execute() override
virtual StatusCode initialize() override
STL class.
PerfMonTestPolyVectorAlg.h Example for the memory optimization tutorial.