Forum > Database
Is TCSVDataSet fixed?
egsuh:
In the older versions, there were memory leak when I created and destroyed TCSVDataSet, which does not happen in the recent version. Has the bug been fixed?
egsuh:
No. I think this is not related with version. I found the memory blocks are not freed when I try to save it to stream. Following code is to send CSVDataSet to web server.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function Taq_r_6.UploadSampleList(tpid: string; tsid: integer; filename:string ): Boolean;var AStrm: TStream; AStrl : TStringList; turl: string; begin AStrl := TStringList.Create; AStrm := TMemoryStream.Create; CSVDSet.CSVOptions.FirstLineAsFieldNames:= True; CSVDSet.LoadFromCSVFile(filename); CSVDSet.SaveToCSVStream(AStrm); // <== Memory leak is reported here. turl:= URL + Format('SetDataSet?sid=%d', [tsid]); Result := HttpPostFile(turl,'SampleList', tpid + '.csv', AStrm, AStrl); AStrm.Position:= 0; AStrl.Free; AStrm.Free;end;
But following codes, which use TBufDataSet instead of TCSVDataSet, does not cause memory leak.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function Taq_r_6.UploadQuestionnaire(tpid: string;DataSet:TDataSet): Boolean;var turl: string; AStrm: TStream; AStrl : TStringList;begin Result:= false; DataSet.First; AStrm := TMemoryStream.Create; AStrl := TStringList.Create; try (DataSet as TBufDataSet).SaveToStream(AStrm); turl := url + Format('SetDataSet?pid=%s', [tpid]); Result := HttpPostFile(tURL, 'qdef', tpid + '.bds', AStrm, AStrl); Result:= uppercase(trim(astrl.Text)) = uppercase(BoolToStr(true, true)); finally AStrl.Free; AStrm.Free; end;end;
What could be possible reason?
egsuh:
Well, following gives me peace ^^.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function Taq_r_6.UploadSampleList(tpid: string; tsid: integer; filename:string ): Boolean;var AStrm: TMemoryStream; AStrl : TStringList; turl: string; begin AStrl := TStringList.Create; AStrm := TMemoryStream.Create; AStrm.LoadFromFile(filename); turl:= URL + Format('SetDataSet?sid=%d', [tsid]); Result := HttpPostFile(turl,'SampleList', tpid + '.csv', AStrm, AStrl); // (URL, FieldName, FileName, Data, ResultData) AStrm.Position:= 0; AStrl.Free; AStrm.Free;end;
PascalDragon:
--- Quote from: egsuh on March 01, 2022, 04:47:48 am ---No. I think this is not related with version. I found the memory blocks are not freed when I try to save it to stream.
--- End quote ---
Would you please provide a simple, self contained example that shows this leak and report it?
Thaddy:
The free's are called in the wrong order:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// mind the creation order: AStrl := TStringList.Create; AStrm := TMemoryStream.Create; // and here you do the free in the wrong order AStrl.Free; AStrm.Free;// which should be in the correct order AStrm.Free; AStrl.Free;First create should be last free'd. IOW LIFO modeled, not FIFO
Since the stream has a dependency on the list that may cause your problem.
It may very well be that there is another bug elsewhere, but you should adhere to Last created should be first to free.
Your code contains a programmer error in the above code.
(By the way,) I would use two try finally's in the above code:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- AStrl := TStringList.Create; try AStrm := TMemoryStream.Create; try..... finally Astrm.Free; end; finally Astrl.free; end;
Navigation
[0] Message Index
[#] Next page