|
1 /* |
|
2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: A class to help track memory leaks. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include "LeakTracker.h" |
|
21 #include "Logger.h" |
|
22 |
|
23 // Globals |
|
24 #ifdef TRACK_LEAKS |
|
25 CLeakTracker* gLeakTracker = NULL; |
|
26 |
|
27 // ----------------------------------------------------------------------------- |
|
28 // CLeakTracker::NewL |
|
29 // |
|
30 // Two-phased constructor. |
|
31 // ----------------------------------------------------------------------------- |
|
32 // |
|
33 CLeakTracker* CLeakTracker::NewL(const TDesC& aSource) |
|
34 { |
|
35 CLeakTracker* self = new (ELeave) CLeakTracker(); |
|
36 |
|
37 CleanupStack::PushL(self); |
|
38 self->ConstructL(aSource); |
|
39 CleanupStack::Pop(); |
|
40 |
|
41 return self; |
|
42 } |
|
43 |
|
44 |
|
45 // ----------------------------------------------------------------------------- |
|
46 // CLeakTracker::CLeakTracker |
|
47 // C++ default constructor can NOT contain any code, that |
|
48 // might leave. |
|
49 // ----------------------------------------------------------------------------- |
|
50 // |
|
51 CLeakTracker::CLeakTracker(): |
|
52 iValues(40) |
|
53 { |
|
54 } |
|
55 |
|
56 |
|
57 // ----------------------------------------------------------------------------- |
|
58 // CLeakTracker::ConstructL |
|
59 // Symbian 2nd phase constructor can leave. |
|
60 // ----------------------------------------------------------------------------- |
|
61 // |
|
62 void CLeakTracker::ConstructL(const TDesC& aSource) |
|
63 { |
|
64 //++pK : Added if condition checking for NULL |
|
65 if ( iSource != NULL ) |
|
66 { |
|
67 iSource = aSource.AllocL(); |
|
68 } |
|
69 } |
|
70 |
|
71 |
|
72 // ----------------------------------------------------------------------------- |
|
73 // CLeakTracker::~CLeakTracker |
|
74 // Deconstructor. |
|
75 // ----------------------------------------------------------------------------- |
|
76 // |
|
77 CLeakTracker::~CLeakTracker() |
|
78 { |
|
79 TBool foundLeak = EFalse; |
|
80 |
|
81 if( iSource != NULL ) |
|
82 { |
|
83 // Report leaks. |
|
84 TPtrC source(*iSource); |
|
85 |
|
86 if( iValues != NULL) |
|
87 { |
|
88 for (TInt i = 0; i < iValues.Count(); i++) |
|
89 { |
|
90 if (iValues[i].value != 0) |
|
91 { |
|
92 FEED_LOG3(_L("Feeds"), _L("Feeds_Leaks.log"), |
|
93 EFileLoggingModeAppend, _L("%S: Leak[%d]: %d"), &source, |
|
94 iValues[i].valueId, iValues[i].value); |
|
95 |
|
96 foundLeak = ETrue; |
|
97 } |
|
98 } |
|
99 |
|
100 // Free the array. |
|
101 iValues.Close(); |
|
102 |
|
103 } |
|
104 |
|
105 |
|
106 gLeakTracker = NULL; |
|
107 |
|
108 if (!foundLeak) |
|
109 { |
|
110 FEED_LOG1(_L("Feeds"), _L("Feeds_Leaks.log"), |
|
111 EFileLoggingModeAppend, _L("%S; No leaks found!!!"), &source); |
|
112 } |
|
113 |
|
114 delete iSource; |
|
115 |
|
116 } |
|
117 |
|
118 |
|
119 |
|
120 } |
|
121 |
|
122 |
|
123 // ----------------------------------------------------------------------------- |
|
124 // CLeakTracker::Increment |
|
125 // |
|
126 // Increments the ref-count on the given class type |
|
127 // ----------------------------------------------------------------------------- |
|
128 // |
|
129 void CLeakTracker::Increment(TInstType aType) |
|
130 { |
|
131 LeakTracker_Value newValue; |
|
132 |
|
133 if (gLeakTracker == NULL) |
|
134 { |
|
135 return; |
|
136 } |
|
137 |
|
138 // Find the value-id |
|
139 for (TInt i = 0; i < iValues.Count(); i++) |
|
140 { |
|
141 if (iValues[i].valueId == aType) |
|
142 { |
|
143 iValues[i].value++; |
|
144 return; |
|
145 } |
|
146 } |
|
147 |
|
148 // The type wasn't found so append a new entry. |
|
149 newValue.valueId = aType; |
|
150 newValue.value = 1; |
|
151 |
|
152 iValues.Append(newValue); |
|
153 // Ignore errors. |
|
154 } |
|
155 |
|
156 |
|
157 // ----------------------------------------------------------------------------- |
|
158 // CLeakTracker::Decrement |
|
159 // |
|
160 // Decrements the ref-count on the given class type |
|
161 // ----------------------------------------------------------------------------- |
|
162 // |
|
163 void CLeakTracker::Decrement(TInstType aType) |
|
164 { |
|
165 if (gLeakTracker == NULL) |
|
166 { |
|
167 return; |
|
168 } |
|
169 |
|
170 // Find the value-id |
|
171 for (TInt i = 0; i < iValues.Count(); i++) |
|
172 { |
|
173 if (iValues[i].valueId == aType) |
|
174 { |
|
175 iValues[i].value--; |
|
176 return; |
|
177 } |
|
178 } |
|
179 } |
|
180 |
|
181 #endif |