Jump to content

Newbie question about Memory Leak with TJSONObject


lfgarrido

Recommended Posts

Hello guys

I have this Login Procedure in LoginForm:

procedure TfrmLogin.btnLoginClick(Sender: TObject);
var
  Return : TJSONObject;
begin
  Return := TJSONObject.Create;
  try
    try
      UniMainModule.RESTClient1.BaseURL := 'http://someurl.com/api';
      UniMainModule.RESTRequest1.Resource := 'login';
      UniMainModule.RESTRequest1.Method := rmPOST;
      UniMainModule.RESTRequest1.Params.ParameterByName('j_username').Value := edtUser.Text;
      UniMainModule.RESTRequest1.Params.ParameterByName('j_password').Value := edtPassword.Text;
      UniMainModule.RESTResponse1.ContentType := 'application/json';
      UniMainModule.RESTRequest1.Execute;

      Return := UniMainModule.RESTResponse1.JSONValue as TJSONObject;
    except
      on E: Exception do 
	  ShowMessage(E.Message);
    end;
  finally
    Return.Free;
  end;
end;

 

But when I shutdown the StandAlone Server I got a Memory Leak:

---------------------------
Unexpected Memory Leak
---------------------------
An unexpected memory leak has occurred. The unexpected small block leaks are:
1 - 12 bytes: TDictionary<Data.Bind.Components.TBasicBindComponent,Data.Bind.ObjectScope.TBindSourceAdapterLink>.TValueCollection x 3, TRESTResponseAdapter.TNotify x 1, TRESTRequestAdapter.TNotify x 1, TRESTComponentNotifyList<REST.Client.TCustomRESTRequest.TNotify> x 1, TCookieManager x 1, TRESTClientAdapter.TNotify x 1, TBindEventList1<System.Integer> x 3, TBindEventList x 9, TRESTComponentNotifyList<REST.Client.TCustomRESTClient.TNotify> x 1, TCredentialsStorage x 1, Unknown x 3
13 - 20 bytes: TValueReaderFunc<System.JSON.TJSONValue> x 1, TValueReaderFunc<System.Classes.TStrings> x 1, TValueReaderFunc<System.Integer> x 2, TCustomRESTRequest.TBody x 1, TBindSourceAdapterLink x 3, TList x 2, TJSONObject x 1, TValueWriterProc<System.string> x 6, TValueReaderFunc<System.string> x 14, UnicodeString x 1, Unknown x 9
21 - 28 bytes: @TRESTComponentAdapter.CreateReadOnlyField$ActRec<System.JSON.TJSONValue> x 1, @TRESTComponentAdapter.CreateReadOnlyField$ActRec<System.Classes.TStrings> x 1, @TRESTComponentAdapter.CreateReadOnlyField$ActRec<System.Integer> x 2, @TRESTComponentAdapter.CreateReadOnlyField$ActRec<System.string> x 8, TRESTRequestAdapter.AddParameterFields.MakeParameterField$ActRec x 2, TRESTClientAdapter.AddPropertyFields$ActRec x 1, UnicodeString x 41, Unknown x 19
29 - 36 bytes: TRESTRequestAdapter.CreatePropertyFields$ActRec x 1, TRESTRequestParameter x 2, @TRESTComponentAdapter.CreateReadWriteField$ActRec<System.string> x 6, TRESTHTTP x 1, TRESTRequestParameterList x 5, TScopeMappings x 6, TNativeImageRecord x 9, UnicodeString x 32
37 - 44 bytes: TRESTComponentAdapter.TReadField<System.JSON.TJSONValue> x 1, TRESTComponentAdapter.TReadField<System.Classes.TStrings> x 1, TRESTComponentAdapter.TReadField<System.Integer> x 2, TList<REST.Client.TCustomRESTRequest.TNotify> x 1, TIPHTTPRequest x 1, TList<System.Net.URLClient.TCertificate> x 1, TList<System.Net.HttpClient.Win.PCERT_CONTEXT> x 1, TCookies x 2, TList<System.Net.URLClient.TCredentialsStorage.TCredential> x 1, TList<Data.Bind.ObjectScope.TBaseObjectBindSource> x 3, TList<System.Classes.TNotifyEvent> x 12, TList<System.Classes.TCollectionItem> x 11, TList<Data.Bind.Components.TBasicBindComponent> x 6, TList<REST.Client.TCustomRESTClient.TNotify> x 1, TList<System.Classes.TComponent> x 14, TUniJSHelper x 1, TRESTComponentAdapter.TReadField<System.string> x 8, TList<System.JSON.TJSONPair> x 1, UnicodeString x 26
45 - 52 bytes: TUniMask x 1, TObjectList<Data.Bind.ObjectScope.TBindSourceAdapterLink> x 3, TObjectList<Data.Bind.ObjectScope.TBindSourceAdapterField> x 3, TRESTComponentAdapter.TReadWriteField<System.string> x 6, UnicodeString x 7, Unknown x 1
53 - 60 bytes: TObjectDictionary<System.string,System.Net.URLClient.TURLClient> x 1, TIPProxyConnectionInfo x 1, TObjectDictionary<Data.Bind.Components.TBasicBindComponent,Data.Bind.ObjectScope.TBindSourceAdapterLink> x 3
61 - 68 bytes: UnicodeString x 5, Unknown x 2
69 - 76 bytes: TStringList x 4, TRESTResponseAdapter.AddPropertyFields$ActRec x 1, UnicodeString x 3
77 - 84 bytes: UnicodeString x 3
85 - 92 bytes: UnicodeString x 1, Unknown x 2
101 - 108 bytes: TWinHTTPResponse x 1
109 - 116 bytes: UnicodeString x 2
117 - 124 bytes: UnicodeString x 4, Unknown x 1
133 - 140 bytes: Unknown x 2
157 - 172 bytes: TWinHTTPRequest x 1, TWinHTTPClient x 1
173 - 188 bytes: TUniNativeImageList x 1
189 - 204 bytes: TSubRESTResponseBindSource x 1, TSubRESTRequestBindSource x 1, TSubRESTClientBindSource x 1, TRESTClient x 1
205 - 220 bytes: UnicodeString x 1, Unknown x 1
221 - 236 bytes: TRESTResponse x 1
253 - 268 bytes: TRESTRequest x 1
269 - 284 bytes: Unknown x 1
301 - 316 bytes: TUniMainModule x 1
349 - 380 bytes: TRESTResponseAdapter x 1, TRESTRequestAdapter x 1, TRESTClientAdapter x 1
The sizes of unexpected leaked medium and large blocks are: 4396, 8748

 

If I just comment this line bellow keeping Return Object empty I don't get a Memory Leak message

// Return := UniMainModule.RESTResponse1.JSONValue as TJSONObject;

 

I know that is not a question directly about uniGUI, but I realy appreciate some tips and best pratices.

Tks

Link to comment
Share on other sites

Thanks for the answer @Jean-Marc Kiener but I still got a memory leak even commenting the unnecessary create of Return object.

But used https://jsontodelphi.com/ to create a class for representing my JSON, and I did with successfull without memory leaks.

I will keep my study to undertand what happened with my previous approach to avoid this in the future.

Tks again!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...