Title: RtcFunction result from FromJSON Post by: Theprasit on January 14, 2015, 03:53:42 PM Hi Danijel,
I need to return a result as TRtcRecord to use with RtcScript. The return result was create using TRtcRecord.FromJSON(). The function return is ok but I got memory leak from FastMM4. Following is my code:- Code: procedure TrfCbtComm.ExecGetPlantInfo(Sender: TRtcConnection; Param: TRtcFunctionInfo; Result: TRtcValue); I tried several coding using copyOf or .asObject but not success. Do you have any advises? Regards, Theprasit Title: Re: RtcFunction result from FromJSON Post by: D.Tkalcec (RTC) on January 14, 2015, 06:26:42 PM Using "asJSON", you can simply assign the JSON String to the result, like this:
Result.asJSON := j_son; Alternatively, if you KNOW that your JSON String contains a record, you can do this: Result.asObject := TRtcRecord.FromJSON(j_son); Best Regards, Danijel Tkalcec Title: Re: RtcFunction result from FromJSON Post by: Theprasit on January 15, 2015, 09:51:56 AM Hi Danijel,
Sorry for late response, have to do testing in several cases. Per your answer, I resolved the issue about return result from TRtcRecord.FromJSON but a memory leak still there. After do more debugging, I found a cause that came from RtcScriptEngine. (I use RtcDataProvider to call script that call RtcFunction, so debugging is a bit difficult). When using RtcScriptEngine.Execute, I used a global VAR of TRtcValue to receive a result return from script engine, now I realize that return result from RtcScriptEngine is seem like return result from RTCFunction, mean RTC will create not me, so I changed the code as in the following and the memory leak is gone. Code: TqslHtmlCommandDispatcher = class(TComponent) As per this issue, could you clarify me about these questions:- 1. TRtcRecord.FromJSON(j_son) is a class method, that will create an instance when use. If we already have an instance, we have to use .asJSON instead? 2. If we use global var to received return result from RtcScriptEngine, we need to free it before next received. I tried using FScriptResult:= nil; is not enough, still got memory leak. Thank you for your great product and support Best Regards, Theprasit Title: Re: RtcFunction result from FromJSON Post by: D.Tkalcec (RTC) on January 15, 2015, 09:02:17 PM 1. TRtcRecord.FromJSON class method will create a new TRtcRecord instance, but using "asRecord" to assign an instance will create a copy and assign that copy (not the original), which is why using "asRecord" to assign an instance created with TRtcRecord.FromJSON generates a memory leak. Using "asObject" to assign an instance created with "TRecord.FromJSON" does not generate a memory leak because the original instance will be assigned. But if you have a JSON String, instead of creating an instance of TRtcRecord using "TRtcRecord.FromJSON" and assigning that instance with "asObject", you can simply assign the JSON String with "asJSON".
2. RtcScriptEngine.Compile creates an instance of TRtcValue, which can be passed over to RtcScriptEngine.Execute for execution. In this execution process, the compiled script object will be destroyed by the execution engine, generating a new TRtcValue instance containing the result. Naturally, if you do not need the result, you should free the object returned. But, I do not recommend using a global variable nor a private object member to store the result of the Script Execute method. I would use a local variable instead and Free it immediately after the execution, like this: procedure TqslHtmlCommandDispatcher.ExecScript(const aScript: string); var FScriptResult: TRtcValue; begin FScriptResult:= FScriptEngine.Execute(FDataServer, FScriptEngine.Compile(aScript)); FreeAndNil(FScriptResult); end; If you need the result (normally the result would be a new string), you could change the procedure into a function, like this: function TqslHtmlCommandDispatcher.ExecScript(const aScript: string):string; var FScriptResult: TRtcValue; begin FScriptResult:=FScriptEngine.Execute(FDataServer, FScriptEngine.Compile(aScript)); Result:=FScriptResult.asText; FreeAndNil(FScriptResult); end; Best Regards, Danijel Tkalcec Title: Re: RtcFunction result from FromJSON Post by: Theprasit on January 18, 2015, 12:50:36 PM Hi Danijel,
Thank you for your clarified answer, I aware of using global var and will changed code according to your advice. Thank you and Best Regards, Theprasit |