Skip to content

Commit

Permalink
Action List Update calls are checking JSON repeatedly causing high CP…
Browse files Browse the repository at this point in the history
…U usage and when JSON is valid, the parsed JSON response is leaked leading to Out of Memory error. Suggested change to only validate JSON on MemoJSON changed and free the leaked TJSONObject in TPkgJsonMapper.IsValid. ARC has been deprecated, so instances need to be managed.
  • Loading branch information
MarkRSill committed Mar 27, 2021
1 parent 0777630 commit 601fb67
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions Generator GUI/MainFormU.fmx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ object MainForm: TMainForm
'"'
''
'See also the possibilities in the MenuBar')
OnChangeTracking = MemoJSONChangeTracking
Align = Client
Size.Width = 613.000000000000000000
Size.Height = 355.000000000000000000
Expand Down
16 changes: 15 additions & 1 deletion Generator GUI/MainFormU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ TMainForm = class(TForm)
procedure Label1Click(Sender: TObject);
procedure ActionList1Update(Action: TBasicAction; var Handled: Boolean);
procedure EditClassNameChange(Sender: TObject);
procedure MemoJSONChangeTracking(Sender: TObject);
private type
TValidationTypes = (vtUnchecked, vtValid, vtInvalid);
private
{ Private declarations }
FCheckVersionResponse: TRelease;
FJsonMapper: TPkgJsonMapper;
FIsValid: TValidationTypes;
public
{ Public declarations }
end;
Expand Down Expand Up @@ -211,10 +215,20 @@ procedure TMainForm.ActionList1Update(Action: TBasicAction; var Handled: Boolean
if not OutputFormatDict.TryGetValue(TabControl1.ActiveTab, OutputFormat) then
OutputFormat := nil;

actConvert.Enabled := FJsonMapper.IsValid(MemoJSON.Text.Trim);
if FIsValid = vtUnchecked then
if FJsonMapper.IsValid(MemoJSON.Text.Trim) then
FIsValid := vtValid
else
FIsValid := vtInvalid;
actConvert.Enabled := FIsValid = vtValid;
actSaveAs.Enabled := (OutputFormat <> nil) and (actConvert.Enabled);
end;

procedure TMainForm.MemoJSONChangeTracking(Sender: TObject);
begin
FIsValid := vtUnchecked;
end;

procedure TMainForm.actOnlineValidationExecute(Sender: TObject);
const
JsonValidatorUrl = 'https://jsonformatter.curiousconcept.com/?data=%s&process=true';
Expand Down
7 changes: 6 additions & 1 deletion Lib/Pkg.Json.Mapper.pas
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,13 @@ function TPkgJsonMapper.GetJsonType(aJsonValue: TJsonValue): TJsonType;
end;

function TPkgJsonMapper.IsValid(aJsonString: string): boolean;
var
Value: TJSONValue;
begin
Result := TJSONObject.ParseJSONValue(aJsonString) <> nil;
Value := TJSONObject.ParseJSONValue(aJsonString);
Result := Value <> nil;
if Result then
Value.Free;
end;

function TPkgJsonMapper.LoadFormFile(aJsonFile: string): TPkgJsonMapper;
Expand Down

0 comments on commit 601fb67

Please sign in to comment.