#1 2021-10-29 17:05:03

China Lee
Member
Registered: 2021-10-28
Posts: 4

how to free jo:Variant; TDocVariant.New(jo);;

hi,every body.
how to  free   jo:Variant;  TDocVariant.New(jo);  jo.add(_Json('{"A":"11"}'));    Ctxt.OutContent :=   VariantSaveJson(_Obj(['code','1','msg','请求成功,Prc_WeiXin_DataList_Cust','data',+jo]));
i am try to Freeandnil(Jo),but error.Cause memory leak。
like this "13 - 20 bytes: AnsiString x 2"
I found that "_Json" caused the error. How can I fix this problem
who knows?
thank you

------------------------------------------
this is all code

--------------------------
function THttpServer.Prc_WeiXin_DataList_Cust(Ctxt: THttpServerRequest;
  aInHeaderSL: TRawUTF8List; const aRemoteIP: RawUTF8): cardinal;
var
  doc: TDocVariantData;
  expanded: Integer;
iRequestType, iRequestCode, vUserCode, dDate, PageIndex, PageSize, vExpression,D_URL: string;
  D_CountMax,D_PageMax,D_JsonData,D_PageInfo:string;
  aParam:SockString;
  aParameters: PUTF8Char;
  aName, aValue,D_Result: RawUTF8;
  jo:Variant;
begin
try
try
Ctxt.OutContentType := 'application/json';
    TDocVariant.New(jo);;
with DB(DefaultDB).CreateSpSMQuery() do
  begin
    with  Spquery do
    begin
      StoredProcName := 'Prc_WeiXin_DataList_Cust';
      PrepareSQL;
      ParamByName ('iRequestType').AsString :='3'; //不走条码为0
      ParamByName ('iRequestCode').AsString :='1'; //samp_sn
      ParamByName ('vUserCode').AsString := 'admin';
      ParamByName ('dDate').AsString := '2021-01-01'; //不是急查
      ParamByName ('iPageIndex').AsString :=  '1'; //inst_code
      ParamByName ('iPageSize').AsString := '2'; //inst_code
      ParamByName ('vExpression').AsString :=  '1=1'; //inst_code

      Execute;
        D_CountMax :=  ParamByName('iCountRows').AsString ;
          D_PageMax :=  ParamByName('iCountPage').AsString ;
     D_PageInfo := '"CountMax":"'+D_CountMax+'","PageMax":"'+D_PageMax+'",';
        D_Pageinfo := D_PageInfo + '"PageIndex":"'+ '1'+'","PageSize":"'+'2'+'"';
       if RecordCount >= 1 then
      begin
               if (D_CountMax = '0') and (D_PageMax = '0') then
        begin
          D_Result := '"data":[]';
        end
        else
        begin
          D_Result := '"data":'+ TDBDatasetToJson.DataSetToRawUTF8Json(SpQuery,true);
           //D_Result := '"data":'+DataSetToJSON(SpQuery); //MMT自带的
        end;
        D_Result :='{' + D_pageinfo +','+ D_Result + '}';
          Result := STATUS_SUCCESS;
            jo.add(_json(D_Result));

        Ctxt.OutContent :=   VariantSaveJson(_Obj(['code','1','msg','请求成功,Prc_WeiXin_DataList_Cust','data',+jo]));
      end
      else if RecordCount < 1 then
      begin
        Result := STATUS_SUCCESS;
        D_Result := '{' + D_pageinfo +',"data":[]}';
           jo.add(_json(D_Result));
          Ctxt.OutContent :=  VariantSaveJson(_Obj(['code','0','msg','请求失败,Prc_WeiXin_DataList_Cust','data',+jo]));
      end;

    end;
end;
  except on E: Exception do
  begin
  Ctxt.OutContent :=  VariantSaveJson(_Obj(['code','0','msg','请求失败,Prc_WeiXin_DataList_Cust','data',e.Message]));
   Set_Log('方法:'+Ctxt.Method+'  URL:'+Ctxt.URL+' [ERR出参] ',Ctxt.OutContent,1,form1.mmo1,info);
  end;
end;
finally
 
end;
end;

Last edited by China Lee (2021-10-29 17:34:49)

Offline

#2 2021-10-29 19:39:38

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: how to free jo:Variant; TDocVariant.New(jo);;

No need to free.

From docs:

4.4.1. TDocVariant documents
...
Instance life-time is managed by the compiler (like any other variant type), without the need to use interfaces or explicit try..finally blocks;

https://synopse.info/files/html/Synopse … 01.18.html

Offline

#3 2021-10-30 00:53:53

China Lee
Member
Registered: 2021-10-28
Posts: 4

Re: how to free jo:Variant; TDocVariant.New(jo);;

if not free.
However, there is a memory leak.

How to deal with the memory leak?

Offline

#4 2021-10-31 11:42:54

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: how to free jo:Variant; TDocVariant.New(jo);;

Hello China Lee,

Please follow the forum rules and don't post code directly in the forum thread.
Prepare an external reproducible minimum example.
You code is weird e.g. the 'data',+jo])); expression weems pretty wrong to me.

Offline

#5 2021-10-31 15:34:18

China Lee
Member
Registered: 2021-10-28
Posts: 4

Re: how to free jo:Variant; TDocVariant.New(jo);;

hello ab,

Whatapp can't be used in CN. I don't know the rules of French BBS ,Sorry.

Next time I paste only the key code

My problem is solved

Use tdocvariant. Newjson (d_result);

No memory leaks.


thank you

Last edited by China Lee (2021-10-31 15:37:15)

Offline

#6 2021-10-31 17:44:16

tbo
Member
Registered: 2015-04-20
Posts: 353

Re: how to free jo:Variant; TDocVariant.New(jo);;

China Lee wrote:

I don't know the rules of French BBS ,Sorry.

You can find the "Rules" button in the toolbar at top of the page.
Here is the link to the forum rules page.

With best regards
Thomas

Offline

Board footer

Powered by FluxBB