Forum > General

Find text in memo gives memory leak

(1/2) > >>

Hansvb:
Hi,

On this website
--- Quote ---https://wiki.freepascal.org/TMemo
--- End quote ---
I found how to search for a text in a memo. I copied this almost exactly. Searching for a text in the memo works fine but when I close the app I have a memory leak. If I remove or don't use the search function then I don't have a memory leak.
However, I don't see what could be causing a memory leak in this function.
what goes wrong here?


--- 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";}};} ---procedure TFrmMain.ButtonSearchClick(Sender: TObject);const  SearchStr : String = '';  SearchStart : Integer = 0;begin  // heeft een geheugenlek... ???  if SearchStr <> EditSearchFormatedMemo.Text then begin    SearchStart := 0;    SearchStr := EditSearchFormatedMemo.Text;  end;   SearchStart := FindInMemo(MemoFormatted, SearchStr, SearchStart + 1);  if SearchStart > 0 then begin    Caption := 'Found at position[' + IntToStr(SearchStart) + ']!';  end  else begin    Caption := 'No further finds.';  end;end; function TFrmMain.FindInMemo(aMemo: TMemo; aString: String; StartPos: Integer  ): Integer;begin  // UpperCase. niet hoofdleter gevoelig voorlopig.  Result := PosEx(UpperCase(aString), UpperCase(aMemo.Text), StartPos);  //Result := PosEx(aString, aMemo.Text, StartPos);  if Result > 0 then begin    aMemo.SelStart := UTF8Length(PChar(aMemo.Text), Result -1);    aMemo.SelLength := Length(aString);    aMemo.SetFocus;  end;end;

howardpc:
There seems to be a minor bug in LazFileUtils, specifically in the


--- 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 ConvertUTF16ToUTF8(Dest: PChar; DestCharCount: SizeUInt;  Src: PWideChar; SrcWideCharCount: SizeUInt; Options: TConvertOptions;  out ActualCharCount: SizeUInt): TConvertResult; althouugh I am not sure I have traced through the debugger correctly.
The function is very clever, and relies on casts of string to PChar, and also casts the
--- 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";}};} ---SrcI'th widechar to a Word value which is used in a comparison. Perhaps the output string is not finalised correctly?
I am not clever enough to see what is causing the leak, but ansistring/widestring conversion is a pretty specialist topic, and I am sure it requires an expert in cross-platform string handling to get it right, since all strings (except shortstrings) have automatic memory management by the compiler. So things happen "behind your back", and in this instance perhaps the timing of the auto-memory managment works against the logic of the function?
There is no bug in the code you posted that I can see.

dsiders:

--- Quote from: howardpc on March 28, 2023, 08:03:52 pm ---There seems to be a minor bug in LazFileUtils, specifically in the

--- 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 ConvertUTF16ToUTF8(Dest: PChar; DestCharCount: SizeUInt;  Src: PWideChar; SrcWideCharCount: SizeUInt; Options: TConvertOptions;  out ActualCharCount: SizeUInt): TConvertResult;
--- End quote ---

Did you post a bug report?

howardpc:
No because I cannot accurately pinpoint where the bug lies, or even if it is in that particular function.

Bart:
Confirmed with more simple program:

--- 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";}};} ---program test;{$mode objfpc}{$h+} uses  Classes;var  SL: TStringList;procedure Foo;const  S: String = '';begin  S := SL.Text;end;begin  SL := TStringList.Create;  SL.Add('1');  Foo;  SL.Free;end.
--- Code: ---C:\Users\Bart\LazarusProjecten>ConsoleProjecten\test
Heap dump by heaptrc unit of C:\Users\Bart\LazarusProjecten\ConsoleProjecten\test.exe
116 memory blocks allocated : 2817/3104
115 memory blocks freed     : 2801/3088
1 unfreed memory blocks : 16
True heap size : 262144 (112 used in System startup)
True free heap : 261904
Should be : 261920
Call trace for block $015E1338 size 16
  $00401768  Foo,  line 13 of test.pas
  $004017F3  $main,  line 18 of test.pas

--- End code ---

Bart

Navigation

[0] Message Index

[#] Next page

Go to full version