forked from thargor6/mb3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonitor.pas
281 lines (244 loc) · 6.5 KB
/
Monitor.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
////////////////////////////////////////////////////////////////////////////////
//
// Monitor.pas - Memory monitor
// ----------------------------
// Version: 2003-02-05
// Maintain: Michael Vinther | mv@logicnet·dk
//
// Last changes:
// Monitor is auto-enabled when assertions and debugging is enabled.
//
unit Monitor;
{DEFINE MonitorObjects} // Define MonitorObjects to use object monitor
{DEFINE UseMonitorTags} // Define UseMonitorTags to give each object a tag #
{D-} // Turn debug info off
interface
uses Classes;
{$IFDEF MonitorObjects}
const
MemLogFile = 'memlog.txt';
type
TMonitorObject = class(TObject)
{$IFDEF UseMonitorTags}
private
MonitorTag : Integer;
{$ENDIF}
public
constructor Create;
destructor Destroy; override;
end;
procedure GetMem(var P; Size: Integer);
procedure FreeMem(const P; Size: Integer=-1);
{$ELSE}
type TMonitorObject = TObject;
{$ENDIF}
// Return list of active objects and memory blocks
function GetMonitorLog(FreeObjects: Boolean=False): TStrings;
// Call example: Assert(MonitorComponent(Self)) in component constructor.
// This will ensure that the monitor is removed when assertions are turned off.
function MonitorComponent(Component: TComponent): Boolean;
implementation
{$IFNDEF MonitorObjects}
function GetMonitorLog(FreeObjects: Boolean): TStrings;
begin
Result:=nil;
end;
function MonitorComponent(Component: TComponent): Boolean;
begin
Result:=True;
end;
{$ELSE}
uses Windows, Contnrs, MMSystem, SysUtils, Dialogs, FileUtils, SyncObjs;
var
PointerList, ObjectList : TStringList;
Log : TStringList;
Sync : TCriticalSection;
TagNumber : Integer = 0;
function GetMonitorLog(FreeObjects: Boolean): TStrings;
var
I, Count : Integer;
Str : string;
begin
Result:=TStringList.Create;
Sync.Acquire;
try
Result.Assign(Log);
for I:=ObjectList.Count-1 downto 0 do
try
Str:=ObjectList[I];
if FreeObjects then ObjectList.Objects[I].Free;
Result.Add('At $'+Str+' not freed');
except
Result.Add('At $'+ObjectList[I]+' Inherited destructor not called (or project needs rebuilding)');
end;
for I:=0 to PointerList.Count-1 do Result.Add('At $'+PointerList[I]+' bytes not freed');
finally
Sync.Release;
end;
TStringList(Result).Sort;
I:=0;
while I<Result.Count-1 do
begin
Count:=1;
while (I<Result.Count-1) and (Result[I]=Result[I+1]) do
begin
Result.Delete(I+1);
Inc(Count);
end;
if Count>1 then Result[I]:=Result[I]+' * '+IntToStr(Count);
Inc(I);
end;
end;
//==============================================================================================================================
// TComponentMonitor
//==============================================================================================================================
type
TComponentMonitor = class(TComponent)
public
constructor Create(AOwner: TComponent; Addr: Cardinal); reintroduce;
destructor Destroy; override;
end;
function MonitorComponent(Component: TComponent): Boolean;
var
Addr: Cardinal;
begin
asm
mov eax,[ebp+4]
mov Addr,eax
end;
Result:=True;
TComponentMonitor.Create(Component,Addr);
end;
constructor TComponentMonitor.Create(AOwner: TComponent; Addr: Cardinal);
begin
inherited Create(AOwner);
Sync.Acquire;
try
{$IFDEF UseMonitorTags}
MonitorTag:=TagNumber;
Inc(TagNumber);
ObjectList.AddObject(IntToHex(Addr,8)+', time='+IntToStr(TimeGetTime)+', tag='+IntToStr(MonitorTag)+': '+Owner.ClassName,Self);
{$ELSE}
ObjectList.AddObject(IntToHex(Addr,8)+': '+Owner.ClassName,Self);
{$ENDIF}
finally
Sync.Release;
end;
end;
destructor TComponentMonitor.Destroy;
var
I : Integer;
begin
Sync.Acquire;
try
I:=ObjectList.IndexOfObject(Self);
if I>=0 then ObjectList.Delete(I);
finally
Sync.Release;
end;
inherited;
end;
//==============================================================================================================================
// TMonitorObject
//==============================================================================================================================
constructor TMonitorObject.Create;
var
Addr: Cardinal;
begin
asm
mov eax,[ebp+4]
mov Addr,eax
end;
inherited;
Sync.Acquire;
try
{$IFDEF UseMonitorTags}
MonitorTag:=TagNumber;
Inc(TagNumber);
ObjectList.AddObject(IntToHex(Addr,8)+', time='+IntToStr(TimeGetTime)+', tag='+IntToStr(MonitorTag)+': '+ClassName,Self);
{$ELSE}
ObjectList.AddObject(IntToHex(Addr,8)+': '+ClassName,Self);
{$ENDIF}
finally
Sync.Release;
end;
end;
destructor TMonitorObject.Destroy;
var
I : Integer;
begin
Sync.Acquire;
try
I:=ObjectList.IndexOfObject(Self);
if I>=0 then ObjectList.Delete(I)
else Log.Add(ClassName+' at $'+IntToHex(Integer(Self),8)+' allready freed or inherited constructor/destructor not called')
finally
Sync.Release;
end;
inherited;
end;
//==============================================================================================================================
procedure GetMem(var P; Size: Integer);
var
Addr: Cardinal;
begin
asm
mov eax,[ebp+4]
mov Addr,eax
end;
System.GetMem(Pointer(P),Size);
Sync.Acquire;
try
{$IFDEF UseMonitorTags}
PointerList.AddObject(IntToHex(Addr,8)+', tag='+IntToStr(TagNumber)+': '+IntToStr(Size),Pointer(P));
Inc(TagNumber);
{$ELSE}
PointerList.AddObject(IntToHex(Addr,8)+': '+IntToStr(Size),Pointer(P));
{$ENDIF}
finally
Sync.Release;
end;
end;
procedure FreeMem(const P; Size: Integer=-1);
var
I : Integer;
begin
Sync.Acquire;
try
I:=PointerList.IndexOfObject(Pointer(P));
if I=-1 then
begin
if Size<>-1 then Log.Add(Format('Data at $%p allready freed',[Pointer(P)]))
end
else PointerList.Delete(I);
finally
Sync.Release;
end;
if Size<>-1 then System.FreeMem(Pointer(P),Size)
else System.FreeMem(Pointer(P));
end;
initialization
ObjectList:=TStringList.Create;
ObjectList.Capacity:=100;
PointerList:=TStringList.Create;
PointerList.Capacity:=100;
Log:=TStringList.Create;
Sync:=TCriticalSection.Create;
finalization
with GetMonitorLog(True) do
begin
if (Count>0) or (GetFileSize(ProgramPath+MemLogFile)<>0) then
try
SaveToFile(ProgramPath+MemLogFile);
except
end;
if Count>0 then
MessageBox(0,PChar(Text),'Monitor has detected a memory leak',MB_OK or MB_ICONWARNING);
Free;
end;
Sync.Free;
Log.Free;
PointerList.Free;
ObjectList.Free;
{$ENDIF}
end.