Rafael Mattos 3 Posted March 29, 2019 Hi, how are you? I have a get method with the return of a TJSONArray and that object is not destroyed. And when I destroy at the end of the function and raised an exception. How do I release this object from memory? Thanks in advance. type [Path('product')] TProductResource = class protected [Context] Token: TMARSToken; public [GET, PermiteAll] function GetProduct([FormParam] ClientId: Integer):TJSONArray; end; function TProductResource.GetProduct(ClientId: Integer): TJSONArray; var xJSONArray: TJSONArray; begin try if Token.Roles[0] = 'client' then xJSONArray:= TBllProduct.Singlenton.RetrieveJSONArray(Token.Claims['Id'].AsInteger) else xJSONArray:= TBllProduct.Singlenton.RetrieveJSONArray(ClientId, Token.Claims['Id'].AsInteger); Result:= xJSONArray; finally //FreeAndNil(xJSONArray); end; end; Share this post Link to post
Bill Meyer 337 Posted March 29, 2019 1 minute ago, Rafael Mattos said: Hi, how are you? I have a get method with the return of a TJSONArray and that object is not destroyed. And when I destroy at the end of the function and raised an exception. How do I release this object from memory? Thanks in advance. The function returns it, so cannot free it. The caller owns the instance, and must free it. Share this post Link to post
Rafael Mattos 3 Posted March 29, 2019 Ok, and where is this caller in the MARS-Curiosity Library? Share this post Link to post
Remy Lebeau 1393 Posted March 31, 2019 (edited) On 3/29/2019 at 12:28 PM, Rafael Mattos said: Ok, and where is this caller in the MARS-Curiosity Library? The caller that Bill refers to is whatever code is calling your TProductResource.GetProduct() method. That code is responsible for freeing the returned array when done using it, eg: Arr := SomeProductResource.GetProduct(ClientId); ... Arr.Free; UNLESS - TBllProduct.Singlenton.RetrieveJSONArray() returns an array that is owned by the Singleton, in which case the code that calls your TProductResource.GetProduct() must not free the array at all. Edited March 31, 2019 by Remy Lebeau Share this post Link to post
Andrea Magni 75 Posted April 1, 2019 (edited) Hi all, @Rafael Mattos, I am fine thanks! I am relatively silent these weeks as I am a bit busier than usual and time is always lacking. Sorry about that. MARS collects result values of your methods and use them to properly serialize them and send them to the client. The result value will be freed by MARS at the end of the "Activation" (the handling of an incoming request down to the sending of the serialized result to the client). If you want to prevent MARS from freeing the instance you are returning, you can decorate your method with the IsReference attribute ( defined in https://github.com/andrea-magni/MARS/blob/master/Source/MARS.Core.Attributes.pas ). [GET, PermitAll, IsReference] function GetProduct([FormParam] ClientId: Integer):TJSONArray; Hint: there was a typo in your PermiAll attribute (PermiteAll) and you can omit the PermitAll attribute as it is the default behavior. Let me know if this solves your problem. Sincerely, Edited April 1, 2019 by Andrea Magni Share this post Link to post
Rafael Mattos 3 Posted April 1, 2019 Thank you to everyone who answered me. @Andrea Magni, the framework does what I thought it did not do. I need to free the TJSONArray from memory. Except that in initial tests it seemed to me that the object remained instantiated, but it does not remain. Thank you! 1 Share this post Link to post