Skip to content

Commit

Permalink
Fixed memory leak in dictionary details
Browse files Browse the repository at this point in the history
  • Loading branch information
Bert Herngreen committed Oct 30, 2019
1 parent 7ff223a commit 8b17a99
Show file tree
Hide file tree
Showing 14 changed files with 1,109 additions and 602 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Test/bin/
*.local
*.identcache
*.dcu
Test/X2LogTest.stat
358 changes: 183 additions & 175 deletions NamedPipeClient/X2LogNamedPipeClient.dproj

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Packages/D10/X2LogJson.dpk
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ package X2LogJson;

requires
rtl,
X2Log;
X2Log,
soaprtl;

contains
X2Log.TextFormatter.Json in '..\..\X2Log.TextFormatter.Json.pas',
Expand Down
1 change: 1 addition & 0 deletions Packages/D10/X2LogJson.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
</DelphiCompile>
<DCCReference Include="rtl.dcp"/>
<DCCReference Include="X2Log.dcp"/>
<DCCReference Include="soaprtl.dcp"/>
<DCCReference Include="..\..\X2Log.TextFormatter.Json.pas"/>
<DCCReference Include="..\..\X2Log.JsonDataObjects.pas"/>
<BuildConfiguration Include="Release">
Expand Down
365 changes: 186 additions & 179 deletions ServiceTest/X2LogServiceTest.dproj

Large diffs are not rendered by default.

864 changes: 642 additions & 222 deletions Test/X2LogTest.dproj

Large diffs are not rendered by default.

Binary file modified Test/X2LogTest.res
Binary file not shown.
22 changes: 1 addition & 21 deletions Test/source/MainFrm.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ object MainForm: TMainForm
end
object tsFile: TTabSheet
Caption = 'File'
ExplicitLeft = 0
ExplicitTop = 0
ExplicitWidth = 0
ExplicitHeight = 0
DesignSize = (
623
299)
Expand Down Expand Up @@ -168,10 +164,6 @@ object MainForm: TMainForm
end
object tsRollingFile: TTabSheet
Caption = 'Rolling File'
ExplicitLeft = 0
ExplicitTop = 0
ExplicitWidth = 0
ExplicitHeight = 0
DesignSize = (
623
299)
Expand Down Expand Up @@ -279,10 +271,6 @@ object MainForm: TMainForm
end
object tsNamedPipe: TTabSheet
Caption = 'Named Pipe'
ExplicitLeft = 0
ExplicitTop = 0
ExplicitWidth = 0
ExplicitHeight = 0
DesignSize = (
623
299)
Expand Down Expand Up @@ -431,10 +419,6 @@ object MainForm: TMainForm
TabOrder = 2
object tsText: TTabSheet
Caption = 'Text'
ExplicitLeft = 0
ExplicitTop = 0
ExplicitWidth = 0
ExplicitHeight = 0
DesignSize = (
623
76)
Expand Down Expand Up @@ -504,10 +488,6 @@ object MainForm: TMainForm
object tsException: TTabSheet
Caption = 'Exception'
ImageIndex = 1
ExplicitLeft = 0
ExplicitTop = 0
ExplicitWidth = 0
ExplicitHeight = 0
DesignSize = (
623
76)
Expand Down Expand Up @@ -706,7 +686,7 @@ object MainForm: TMainForm
Left = 552
Top = 176
Bitmap = {
494C01010200140070000C000C00FFFFFFFFFF10FFFFFFFFFFFFFFFF424D3600
494C01010200140074000C000C00FFFFFFFFFF10FFFFFFFFFFFFFFFF424D3600
0000000000003600000028000000300000000C00000001002000000000000009
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
Expand Down
2 changes: 1 addition & 1 deletion Test/source/MainFrm.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface
Vcl.ImgList,
Vcl.StdCtrls,

X2Log.Intf;
X2Log.Intf, System.ImageList;


type
Expand Down
73 changes: 73 additions & 0 deletions X2Log.Details.Default.pas
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ TX2LogStringDetails = class(TInterfacedObject, IX2LogDetails, IX2LogDetailsTex

{ IX2LogDetails }
function GetSerializerIID: TGUID;
function Clone: IX2LogDetails;

{ IX2LogDetailsText }
function GetAsString: string;
Expand All @@ -41,6 +42,7 @@ TX2LogDictionaryValue = class(TObject)
FValueType: TX2LogValueType;
protected
constructor Create(AValueType: TX2LogValueType; AStream: TStream = nil; ASize: Cardinal = 0); overload; virtual;
function Clone: TX2LogDictionaryValue; virtual; abstract;

procedure LoadFromStream(AStream: TStream; ASize: Cardinal); virtual; abstract;
procedure SaveToStream(AStream: TStream); virtual; abstract;
Expand Down Expand Up @@ -88,6 +90,7 @@ TX2LogDictionaryDetails = class(TInterfacedObject, IX2LogDetails, IX2LogDetail

{ IX2LogDetails }
function GetSerializerIID: TGUID;
function Clone: IX2LogDetails;

{ IX2LogDetailsDictionary }
function GetKeys: TEnumerable<string>;
Expand Down Expand Up @@ -123,6 +126,7 @@ TX2LogBinaryDetails = class(TInterfacedObject, IX2LogDetails, IX2LogDetailsBin

{ IX2LogDetails }
function GetSerializerIID: TGUID;
function Clone: IX2LogDetails;

{ IX2LogDetailsBinary }
function GetAsStream: TStream;
Expand All @@ -148,6 +152,7 @@ TX2LogGraphicDetails = class(TInterfacedObject, IX2LogDetails, IX2LogDetailsGr

{ IX2LogDetails }
function GetSerializerIID: TGUID;
function Clone: IX2LogDetails;

{ IX2LogDetailsGraphic }
function GetAsGraphic: TGraphic;
Expand Down Expand Up @@ -217,6 +222,7 @@ TX2LogDictionaryStringValue = class(TX2LogDictionaryValue)
FValue: string;
protected
constructor Create(const AValue: string); overload;
function Clone: TX2LogDictionaryValue; override;

procedure LoadFromStream(AStream: TStream; ASize: Cardinal); override;
procedure SaveToStream(AStream: TStream); override;
Expand All @@ -232,6 +238,7 @@ TX2LogDictionaryBooleanValue = class(TX2LogDictionaryValue)
FValue: Boolean;
protected
constructor Create(AValue: Boolean); overload;
function Clone: TX2LogDictionaryValue; override;

procedure LoadFromStream(AStream: TStream; ASize: Cardinal); override;
procedure SaveToStream(AStream: TStream); override;
Expand All @@ -247,6 +254,7 @@ TX2LogDictionaryIntValue = class(TX2LogDictionaryValue)
FValue: Int64;
protected
constructor Create(AValue: Int64); overload;
function Clone: TX2LogDictionaryValue; override;

procedure LoadFromStream(AStream: TStream; ASize: Cardinal); override;
procedure SaveToStream(AStream: TStream); override;
Expand All @@ -262,6 +270,7 @@ TX2LogDictionaryFloatValue = class(TX2LogDictionaryValue)
FValue: Extended;
protected
constructor Create(AValue: Extended); overload;
function Clone: TX2LogDictionaryValue; override;

procedure LoadFromStream(AStream: TStream; ASize: Cardinal); override;
procedure SaveToStream(AStream: TStream); override;
Expand All @@ -277,6 +286,7 @@ TX2LogDictionaryDateTimeValue = class(TX2LogDictionaryValue)
FValue: TDateTime;
protected
constructor Create(AValue: TDateTime); overload;
function Clone: TX2LogDictionaryValue; override;

procedure LoadFromStream(AStream: TStream; ASize: Cardinal); override;
procedure SaveToStream(AStream: TStream); override;
Expand Down Expand Up @@ -311,6 +321,12 @@ function TX2LogStringDetails.GetSerializerIID: TGUID;
end;


function TX2LogStringDetails.Clone: IX2LogDetails;
begin
Result := TX2LogStringDetails.Create(FText);
end;


function TX2LogStringDetails.GetAsString: string;
begin
Result := FText;
Expand Down Expand Up @@ -442,6 +458,21 @@ function TX2LogDictionaryDetails.GetSerializerIID: TGUID;
end;


function TX2LogDictionaryDetails.Clone: IX2LogDetails;
var
values: TX2LogValueDictionary;
pair: TPair<string, TX2LogDictionaryValue>;

begin
values := TX2LogValueDictionary.Create([doOwnsValues]);

for pair in FValues do
values.Add(pair.Key, pair.Value.Clone);

Result := TX2LogDictionaryDetails.CreateOwned(values);
end;


function TX2LogDictionaryDetails.GetKeys: TEnumerable<string>;
begin
Result := FValues.Keys;
Expand Down Expand Up @@ -566,6 +597,12 @@ function TX2LogBinaryDetails.GetSerializerIID: TGUID;
end;


function TX2LogBinaryDetails.Clone: IX2LogDetails;
begin
Result := TX2LogBinaryDetails.Create(Data);
end;


function TX2LogBinaryDetails.GetAsStream: TStream;
begin
Data.Position := 0;
Expand Down Expand Up @@ -623,6 +660,12 @@ function TX2LogGraphicDetails.GetSerializerIID: TGUID;
end;


function TX2LogGraphicDetails.Clone: IX2LogDetails;
begin
Result := TX2LogGraphicDetails.Create(FGraphic);
end;


procedure TX2LogGraphicDetails.CopyToClipboard;
var
format: Word;
Expand Down Expand Up @@ -823,6 +866,12 @@ constructor TX2LogDictionaryStringValue.Create(const AValue: string);
end;


function TX2LogDictionaryStringValue.Clone: TX2LogDictionaryValue;
begin
Result := TX2LogDictionaryStringValue.Create(FValue);
end;


procedure TX2LogDictionaryStringValue.LoadFromStream(AStream: TStream; ASize: Cardinal);
begin
Value := TStreamUtil.ReadString(AStream, nil, False, ASize);
Expand Down Expand Up @@ -850,6 +899,12 @@ constructor TX2LogDictionaryBooleanValue.Create(AValue: Boolean);
end;


function TX2LogDictionaryBooleanValue.Clone: TX2LogDictionaryValue;
begin
Result := TX2LogDictionaryBooleanValue.Create(FValue);
end;


procedure TX2LogDictionaryBooleanValue.LoadFromStream(AStream: TStream; ASize: Cardinal);
begin
if ASize <> SizeOf(Boolean) then
Expand Down Expand Up @@ -880,6 +935,12 @@ constructor TX2LogDictionaryIntValue.Create(AValue: Int64);
end;


function TX2LogDictionaryIntValue.Clone: TX2LogDictionaryValue;
begin
Result := TX2LogDictionaryIntValue.Create(FValue);
end;


procedure TX2LogDictionaryIntValue.LoadFromStream(AStream: TStream; ASize: Cardinal);
begin
if ASize <> SizeOf(Int64) then
Expand Down Expand Up @@ -910,6 +971,12 @@ constructor TX2LogDictionaryFloatValue.Create(AValue: Extended);
end;


function TX2LogDictionaryFloatValue.Clone: TX2LogDictionaryValue;
begin
Result := TX2LogDictionaryFloatValue.Create(FValue);
end;


procedure TX2LogDictionaryFloatValue.LoadFromStream(AStream: TStream; ASize: Cardinal);
begin
if ASize <> SizeOf(Extended) then
Expand Down Expand Up @@ -940,6 +1007,12 @@ constructor TX2LogDictionaryDateTimeValue.Create(AValue: TDateTime);
end;


function TX2LogDictionaryDateTimeValue.Clone: TX2LogDictionaryValue;
begin
Result := TX2LogDictionaryDateTimeValue.Create(FValue);
end;


procedure TX2LogDictionaryDateTimeValue.LoadFromStream(AStream: TStream; ASize: Cardinal);
begin
if ASize <> SizeOf(TDateTime) then
Expand Down
1 change: 1 addition & 0 deletions X2Log.Intf.pas
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface
IX2LogDetails = interface
['{86F24F52-CE1F-4A79-936F-A5805D84E18A}']
function GetSerializerIID: TGUID;
function Clone: IX2LogDetails;

property SerializerIID: TGUID read GetSerializerIID;
end;
Expand Down
9 changes: 8 additions & 1 deletion X2Log.Observer.CustomThreaded.pas
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,17 @@ destructor TX2LogObserverWorkerThread.Destroy;


procedure TX2LogObserverWorkerThread.Log(ALevel: TX2LogLevel; ADateTime: TDateTime; const AMessage, ACategory: string; ADetails: IX2LogDetails);
var
details: IX2LogDetails;

begin
details := nil;
if Assigned(ADetails) then
details := ADetails.Clone;

TMonitor.Enter(LogQueue);
try
LogQueue.Enqueue(TX2LogQueueEntry.Create(ALevel, ADateTime, AMessage, ACategory, ADetails));
LogQueue.Enqueue(TX2LogQueueEntry.Create(ALevel, ADateTime, AMessage, ACategory, details));
LogQueueSignal.SetEvent;
finally
TMonitor.Exit(LogQueue);
Expand Down
11 changes: 9 additions & 2 deletions X2Log.Observer.Event.pas
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ constructor TX2LogEventObserver.Create(AOnLog: TX2LogEvent; ALogLevels: TX2LogLe


procedure TX2LogEventObserver.DoLog(ALevel: TX2LogLevel; ADateTime: TDateTime; const AMessage, ACategory: string; ADetails: IX2LogDetails);
var
details: IX2LogDetails;

begin
details := nil;
if Assigned(ADetails) then
details := ADetails.Clone;

if Assigned(FOnLog) then
begin
if RunInMainThread then
Expand All @@ -58,10 +65,10 @@ procedure TX2LogEventObserver.DoLog(ALevel: TX2LogLevel; ADateTime: TDateTime; c
procedure
begin
if Assigned(FOnLog) then
FOnLog(Self, ALevel, ADateTime, AMessage, ACategory, ADetails);
FOnLog(Self, ALevel, ADateTime, AMessage, ACategory, details);
end);
end else
FOnLog(Self, ALevel, ADateTime, AMessage, ACategory, ADetails);
FOnLog(Self, ALevel, ADateTime, AMessage, ACategory, details);
end;
end;

Expand Down
1 change: 1 addition & 0 deletions X2Log.TextFormatter.Json.pas
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ implementation
uses
Soap.EncdDecd,
System.Classes,
System.NetEncoding,
System.SysUtils;


Expand Down

0 comments on commit 8b17a99

Please sign in to comment.