Lazarus

Programming => General => Topic started by: egsuh on April 09, 2025, 01:08:47 pm

Title: Any specific difference between TCSVStream and TMemoryStream?
Post by: egsuh on April 09, 2025, 01:08:47 pm
Hello,

I've written a procedure that converts datasets to TMemoryStream.

Code: Pascal  [Select][+][-]
  1. function Taq_r_5.SetDataSet(querydef, fieldname, filename: string;DataSet:TDataSet): Boolean;
  2. var
  3.    turl, tfn : string;
  4.    AStrm: TMemoryStream;
  5.    AStrl : TStringList;
  6. begin
  7.    Result:= false;
  8.    DataSet.First;
  9.    AStrl := TStringList.Create;
  10.    AStrm := TMemoryStream.Create;
  11.    try
  12.       if DataSet <> nil then begin
  13.          if uppercase(fieldname) = 'SAMPLELIST'
  14.             then (DataSet as TCSVDataSet).SaveToCSVStream(AStrm)   // <==== This is not Freed completely
  15.             else (DataSet as TBufDataSet).SaveToStream(AStrm);
  16.          tfn := filename;
  17.  
  18.          Astrm.Position:= 0;
  19.  
  20.          turl := url + Format('e/SetDataSet?%s', [querydef]);
  21.          Result :=  HttpPostFile(tURL, fieldname, tfn, AStrm, AStrl);
  22.          Result:= uppercase(trim(astrl.Text)) = uppercase(BoolToStr(true, true));
  23.       end;
  24.    finally
  25.       AStrl.Free;
  26.       AStrm.Free;    // <--- it should be freed finally
  27.    end;
  28. end;
  29.  

SaveToStream to a MemoryStream variable works ok.  But SaveToCSVStream to a MemoryStream variable is not cleared at free.
Even though it is not completely freed, it operates correctly.

Any reasons?
Title: Re: Any specific difference between TCSVStream and TMemoryStream?
Post by: Zoran on April 09, 2025, 09:20:15 pm
Hello,

I've written a procedure that converts datasets to TMemoryStream.

Code: Pascal  [Select][+][-]
  1. function Taq_r_5.SetDataSet(querydef, fieldname, filename: string;DataSet:TDataSet): Boolean;
  2. var
  3.    turl, tfn : string;
  4.    AStrm: TMemoryStream;
  5.    AStrl : TStringList;
  6. begin
  7.    Result:= false;
  8.    DataSet.First;
  9.    AStrl := TStringList.Create;
  10.    AStrm := TMemoryStream.Create;
  11.    try
  12.       if DataSet <> nil then begin
  13.          if uppercase(fieldname) = 'SAMPLELIST'
  14.             then (DataSet as TCSVDataSet).SaveToCSVStream(AStrm)   // <==== This is not Freed completely
  15.             else (DataSet as TBufDataSet).SaveToStream(AStrm);
  16.          tfn := filename;
  17.  
  18.          Astrm.Position:= 0;
  19.  
  20.          turl := url + Format('e/SetDataSet?%s', [querydef]);
  21.          Result :=  HttpPostFile(tURL, fieldname, tfn, AStrm, AStrl);
  22.          Result:= uppercase(trim(astrl.Text)) = uppercase(BoolToStr(true, true));
  23.       end;
  24.    finally
  25.       AStrl.Free;
  26.       AStrm.Free;    // <--- it should be freed finally
  27.    end;
  28. end;
  29.  

SaveToStream to a MemoryStream variable works ok.  But SaveToCSVStream to a MemoryStream variable is not cleared at free.
Even though it is not completely freed, it operates correctly.

Any reasons?

What do you mean? This code does not do anything after calling AStrm.Free, so it doesn't try to access this object after it was freed (and of course it shouldn't try). So how can you tell that the variable operates correctly and that it is not completely freed?

Please try to explain better what you mean.
Title: Re: Any specific difference between TCSVStream and TMemoryStream?
Post by: egsuh on April 10, 2025, 04:49:20 am
Quote
So how can you tell that the variable operates correctly and that it is not completely freed?

Heaptrack says some memories are not freed.
TinyPortal © 2005-2018