Recent

Author Topic: Memory leak with a TCSVDataset  (Read 1107 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 934
Memory leak with a TCSVDataset
« on: February 01, 2026, 06:50:37 pm »
Hi,

I've been looking for a memory leak all day. it seems that this is caused by a TCSVDataset. See below. This minimal code has a memory leak. I suspect that with the line
Code: Pascal  [Select][+][-]
  1. fDataset.Active:= True;
something happens in the background that will not be released later. Because if I don't implement that line, I don't have a leak. If that line does execute, then there is a leak.

I can't trace this back. What am I forgetting?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   csvdataset;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure FormDestroy(Sender: TObject);
  19.   private
  20.     fDataset: TCSVDataset;
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. begin
  36.   fDataset:= TCSVDataset.Create(Nil);  // Self geeft ook een gehuegenlek
  37.   fDataset.CSVOptions.FirstLineAsFieldNames:= True;
  38.   fDataset.CSVOptions.Delimiter:= ',';
  39.   fDataset.CSVOptions.QuoteChar:= '"';
  40.   fDataset.CSVOptions.IgnoreOuterWhitespace := True;
  41.   fDataset.FileName:= 'C:\Users\Hans\AppData\Roaming\Gwsw-exporter\686_regels.csv';
  42.   fDataset.Active:= True;  //<----------- Something happens here that is later not released. ?
  43. end;
  44.  
  45. procedure TForm1.FormDestroy(Sender: TObject);
  46. var
  47.   i: Integer;
  48. begin
  49.   if fDataset.Active then begin
  50.     i:= fDataset.FieldCount;
  51.  
  52. {    for i:= fDataset.Fields.Count - 1 downto 0 do
  53.       fDataset.Fields[i].Free;
  54.  
  55.     fDataset.Fields.Clear;}  // --> doesn't help
  56.  
  57.     fDataset.Active:= False;
  58.     fDataset.Close;
  59.  
  60.   //fDataset.Clear;
  61.   //fDataset.Destroy;
  62.   end;
  63.  
  64.   if assigned(fDataset) then FreeAndNil(fDataset);
  65.   //fDataset.Free;
  66. end;
  67.  
  68. end.
  69.  


Edit: Windows 11, Lazarus 4.4
« Last Edit: February 01, 2026, 06:56:04 pm by Hansvb »

paweld

  • Hero Member
  • *****
  • Posts: 1691
Re: Memory leak with a TCSVDataset
« Reply #1 on: February 01, 2026, 07:59:12 pm »
How many times do you press the button? - Each time you press the button, you create a new instance of the component, and in form destroy you release only the last instance created. Pressing the button more than once will cause memory leaks.
A good rule of thumb is: if you release a component in the OnDestroy event, create it in OnCreate.
Best regards / Pozdrawiam
paweld

wp

  • Hero Member
  • *****
  • Posts: 13646
Re: Memory leak with a TCSVDataset
« Reply #2 on: February 01, 2026, 08:04:37 pm »
Found the attached little demo project on my HD. For testing, I am compiling the application, running it and closing it immediately after the form is shown, I am doing nothing else inside the application.

* When I use Laz/main + FPC 3.2.2 in this test, several memory leaks are reported.
* Switched to an installation of Laz/main + FPC 3.3.1 - no more memory leak.
* Laz/main + FPC/fixes - no more memory leak
* Laz 4.4 + FPC 3.2.4RC1 - no memory leak either.

Therefore I would conclude that TCSVDataset does have a memory leak in FPC 3.2.2, but it has been fixed in FPC 3.2.4RC1 and newer.

Hansvb

  • Hero Member
  • *****
  • Posts: 934
Re: Memory leak with a TCSVDataset
« Reply #3 on: February 01, 2026, 09:01:14 pm »
@Paweld, this was just short quick code with which I can repeat it. I only press the button once. But the example could have been better.

@wp, I thought I saw in csvdataset.pp in the destroy a free to few but because I can't test  that and I'm not very sure I didn't mention that. I'll see tomorrow or later this week if I can manage to use a newer pascal version. And otherwise I can probably use buffdataset as a workaround. (Not tried yet).

paweld

  • Hero Member
  • *****
  • Posts: 1691
Re: Memory leak with a TCSVDataset
« Reply #4 on: February 01, 2026, 09:32:30 pm »
You can use TSdfDataset as a replacement for TCSVDataset.
Best regards / Pozdrawiam
paweld

jamie

  • Hero Member
  • *****
  • Posts: 7891
Re: Memory leak with a TCSVDataset
« Reply #5 on: February 01, 2026, 10:25:10 pm »
I believe I located the issue.

Code: Pascal  [Select][+][-]
  1. procedure TCSVDataPacketReader.StoreFieldDefs(AnAutoIncValue: integer);        
  2.  

In that procedure there is this.
Code: Pascal  [Select][+][-]
  1. FBuilder:=TCSVBuilder.Create;
  2.  

Which in the destructor nor in the current procedure above dose it get freed from what I can see.

Code: Pascal  [Select][+][-]
  1. destructor TCSVDataPacketReader.Destroy;
  2. begin
  3.   FreeAndNil(FCreateFieldDefs);
  4.   If FOwnsOptions then
  5.     FreeAndNil(FOPtions);
  6.   FreeAndNil(Fline);
  7.   FreeAndNil(FParser);
  8.   inherited Destroy;
  9. end;                      
  10.  
  11.  

Unless I am going blinder, I don't see a release of the FBuilder ?

Just an observation.

I did try out the bufdataSet, that seems to work just fine so it's totally related to the CVS thingy..

Jamie
The only true wisdom is knowing you know nothing

dsiders

  • Hero Member
  • *****
  • Posts: 1677
Re: Memory leak with a TCSVDataset
« Reply #6 on: February 01, 2026, 10:49:25 pm »
I believe I located the issue.

Code: Pascal  [Select][+][-]
  1. procedure TCSVDataPacketReader.StoreFieldDefs(AnAutoIncValue: integer);        
  2.  

In that procedure there is this.
Code: Pascal  [Select][+][-]
  1. FBuilder:=TCSVBuilder.Create;
  2.  

Which in the destructor nor in the current procedure above dose it get freed from what I can see.

Code: Pascal  [Select][+][-]
  1. destructor TCSVDataPacketReader.Destroy;
  2. begin
  3.   FreeAndNil(FCreateFieldDefs);
  4.   If FOwnsOptions then
  5.     FreeAndNil(FOPtions);
  6.   FreeAndNil(Fline);
  7.   FreeAndNil(FParser);
  8.   inherited Destroy;
  9. end;                      
  10.  
  11.  

Unless I am going blinder, I don't see a release of the FBuilder ?

Just an observation.

I did try out the bufdataSet, that seems to work just fine so it's totally related to the CVS thingy..

Jamie

Yes, that's exactly what was done in commit 56acf11ec2 back in Mar of 2022.

wp

  • Hero Member
  • *****
  • Posts: 13646
Re: Memory leak with a TCSVDataset
« Reply #7 on: February 01, 2026, 11:36:17 pm »
Indeed, this was an old bug report of mine which I forgot: https://gitlab.com/freepascal.org/fpc/source/-/issues/39607

jamie

  • Hero Member
  • *****
  • Posts: 7891
Re: Memory leak with a TCSVDataset
« Reply #8 on: February 01, 2026, 11:43:49 pm »
Ok, so I wasted that much time finding what was already known about.

So, what's the solution for now, place a corrected copy in the project folder?

I would prefer a new release of the IDE with 3.2.4 in it.

Jamie
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 13646
Re: Memory leak with a TCSVDataset
« Reply #9 on: February 02, 2026, 01:34:00 am »
Ok, so I wasted that much time finding what was already known about.
No, you did not waste any time. Your analysis reminded me of my earlier work, and I finally had the idea to search for "TCSVDataset" among the closed gitlab reports.

So, what's the solution for now, place a corrected copy in the project folder?
Yes, that's what I would do.

egsuh

  • Hero Member
  • *****
  • Posts: 1825
Re: Memory leak with a TCSVDataset
« Reply #10 on: February 02, 2026, 06:28:36 am »
I raised this issue last year.

https://forum.lazarus.freepascal.org/index.php/topic,58510.0.html

Not sure it is solved in FPC 3.2.2, which comes with Lazarus 4.4.

Hansvb

  • Hero Member
  • *****
  • Posts: 934
Re: Memory leak with a TCSVDataset
« Reply #11 on: February 02, 2026, 11:44:01 am »
Quote
Not sure it is solved in FPC 3.2.2, which comes with Lazarus 4.4.

I just tested it. I downloaded FPCUPdeluxe and installed the combination of Lazarus 4.4 with "FPC fixes 3.2". then the bug is gone.
The
Code: Pascal  [Select][+][-]
  1. destructor TCSVDataPacketReader.Destroy;
has an extra line in the 3.2 FPC fixes:
Code: Pascal  [Select][+][-]
  1. FreeAndNil(FBuilder);

Hansvb

  • Hero Member
  • *****
  • Posts: 934
Re: Memory leak with a TCSVDataset
« Reply #12 on: February 03, 2026, 07:52:23 pm »
If I put csvdataset of FPC version 3.2.3 in my project folder structure and compile my application then I no longer have a memory leak when I use a tcsvdataset.

Is that allowed license technically?

cdbc

  • Hero Member
  • *****
  • Posts: 2921
    • http://www.cdbc.dk
Re: Memory leak with a TCSVDataset
« Reply #13 on: February 03, 2026, 08:05:47 pm »
Hi Hans
I'd say __Yes__, being that it's the same /product/ and under the same license as the rest of the /older/ compiler modules.
As long as the units / modules keep their original license, you should be fine to intermix / use them...  :)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Hansvb

  • Hero Member
  • *****
  • Posts: 934
Re: Memory leak with a TCSVDataset
« Reply #14 on: February 03, 2026, 08:08:57 pm »
I don't expect anyone, except myself, to use my little tool, but I do want to put it on codeberg.org hence better safe in terms of license than hassle.

 

TinyPortal © 2005-2018