Jump to content
Rafael Mattos

How to release a return [TJSONArray] object from a get method?

Recommended Posts

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
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
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 by Remy Lebeau

Share this post


Link to post

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 by Andrea Magni

Share this post


Link to post
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!
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×