IsManagedType problem, do i miss something? lazarus / 21-General ---------------------------------------------------------------------- powerpcer · 2024-03-13 15:59 in gdeque.pp i see below: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDeque.ClearSingleDataEntry(Index: SizeUInt);begin  if IsManagedType(T) then  begin    Finalize(FData[Index]);    FillChar(FData[Index], SizeOf(T), 0);  end  else    FData[Index] := default(T);end; procedure TDeque.ClearData;var  i: SizeUint;begin  if IsManagedType(T) then    for i := Low(FData) to High(FData) do      Finalize(FData[i]);  FillChar(FData[Low(FData)], SizeUInt(Length(FData))*SizeOf(T), 0);end;     from https://www.freepascal.org/docs-html/ref/refse20.html and https://www.freepascal.org/daily/doc/rtl/system/ismanagedtype.html i got IsManagedType(T) returns True if the type T is a managed type Existing code conflicts with the document. so, do i miss something? ---------------------------------------------------------------------- marcov · 2024-03-13 16:08 If they are managed they are finalized before being zeroed, I don't see what the problem is you are hinting at ? ---------------------------------------------------------------------- Thaddy · 2024-03-13 17:07 So basically you want to mis-manage a managed type?  What is this then?Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDeque.ClearData;var  i: SizeUint;begin  if IsManagedType(T) then    for i := Low(FData) to High(FData) do      Finalize(FData[i]);  FillChar(FData[Low(FData)], SizeUInt(Length(FData))*SizeOf(T), 0);end; You wrote that, I did not...  You are mutilating the reference count. Use the Default() intrinsic. ---------------------------------------------------------------------- ASerge · 2024-03-13 17:59 Quote from: Thaddy on March 13, 2024, 06:07:40 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDeque.ClearData;var  i: SizeUint;begin  if IsManagedType(T) then    for i := Low(FData) to High(FData) do      Finalize(FData[i]);  FillChar(FData[Low(FData)], SizeUInt(Length(FData))*SizeOf(T), 0);end; It's strange that no one noticed an obvious error in the code. When there are no items in the queue, accessing the zero index results in an access error. It is easy to reproduce. Code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TStringDeque = specialize TDeque;var  Q: TStringDeque;  S: string;begin  Q := TStringDeque.Create;  Q.Free;end;causes an error. I would implement it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDeque.ClearData;var  i: SizeUint;begin  if FData = nil then    Exit;  if IsManagedType(T) then    Finalize(FData[Low(FData)], Length(FData));  FillChar(FData[Low(FData)], SizeUInt(Length(FData)) * SizeOf(T), 0);end; ---------------------------------------------------------------------- marcov · 2024-03-13 18:09 Quote from: Thaddy on March 13, 2024, 06:07:40 pmYou wrote that, I did not...  You are mutilating the reference count. Thaddy: There is no reference count for a finalized object.  Serge yes, also filling with #0 is fine for objects, but maybe not for strings that should point to FPC_EMPTYCHAR. Maybe a set of defaults is better. Please file a bug, so that it is not forgotten. gtg. ---------------------------------------------------------------------- Warfley · 2024-03-13 19:57 Quote from: Thaddy on March 13, 2024, 06:07:40 pmUse the Default() intrinsic. Never use Default on a managed type. Default does not execute management operations correctly: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i class operator TTest.Initialize(var instance: TTest);begin  instance.initialized := True;end; var  test: TTest;begin  WriteLn(test.initialized); // "True": Correctly initialized  test:=Default(TTest);   WriteLn(test.initialized); // "False": Not initializedend. Also note that doing an assignment to a finalized object is also most likely an error, because it triggers the copy operator after the finalize operator was triggered, basically the managed version of use- after-free ---------------------------------------------------------------------- Bart · 2024-03-13 21:40 Quote from: marcov on March 13, 2024, 07:09:14 pmPlease file a bug, so that it is not forgotten. gtg. Reported as issue 40695. Patch to solve the range check error is attached in the bugreport. Bart ---------------------------------------------------------------------- powerpcer · 2024-03-14 06:20 Quote from: ASerge on March 13, 2024, 06:59:59 pmQuote from: Thaddy on March 13, 2024, 06:07:40 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDeque.ClearData;var  i: SizeUint;begin  if IsManagedType(T) then    for i := Low(FData) to High(FData) do      Finalize(FData[i]);  FillChar(FData[Low(FData)], SizeUInt(Length(FData))*SizeOf(T), 0);end; It's strange that no one noticed an obvious error in the code. When there are no items in the queue, accessing the zero index results in an access error. It is easy to reproduce. Code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TStringDeque = specialize TDeque;var  Q: TStringDeque;  S: string;begin  Q := TStringDeque.Create;  Q.Free;end;causes an error. I would implement it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDeque.ClearData;var  i: SizeUint;begin  if FData = nil then    Exit;  if IsManagedType(T) then    Finalize(FData[Low(FData)], Length(FData));  FillChar(FData[Low(FData)], SizeUInt(Length(FData)) * SizeOf(T), 0);end;in document said, String is a managed type. but why it should Finalize explicitly? ---------------------------------------------------------------------- bytebites · 2024-03-14 07:28 Quote from: powerpcer on March 14, 2024, 07:20:48 amQuote from: ASerge on March 13, 2024, 06:59:59 pmQuote from: Thaddy on March 13, 2024, 06:07:40 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDeque.ClearData;var  i: SizeUint;begin  if IsManagedType(T) then    for i := Low(FData) to High(FData) do      Finalize(FData[i]);  FillChar(FData[Low(FData)], SizeUInt(Length(FData))*SizeOf(T), 0);end; It's strange that no one noticed an obvious error in the code. When there are no items in the queue, accessing the zero index results in an access error. It is easy to reproduce. Code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TStringDeque = specialize TDeque;var  Q: TStringDeque;  S: string;begin  Q := TStringDeque.Create;  Q.Free;end;causes an error. I would implement it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDeque.ClearData;var  i: SizeUint;begin  if FData = nil then    Exit;  if IsManagedType(T) then    Finalize(FData[Low(FData)], Length(FData));  FillChar(FData[Low(FData)], SizeUInt(Length(FData)) * SizeOf(T), 0);end;in document said, String is a managed type. but why it should Finalize explicitly? Fillchar does not finalize managed types. ---------------------------------------------------------------------- powerpcer · 2024-03-14 09:36 Quote from: bytebites on March 14, 2024, 08:28:34 amQuote from: powerpcer on March 14, 2024, 07:20:48 amQuote from: ASerge on March 13, 2024, 06:59:59 pmQuote from: Thaddy on March 13, 2024, 06:07:40 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDeque.ClearData;var  i: SizeUint;begin  if IsManagedType(T) then    for i := Low(FData) to High(FData) do      Finalize(FData[i]);  FillChar(FData[Low(FData)], SizeUInt(Length(FData))*SizeOf(T), 0);end; It's strange that no one noticed an obvious error in the code. When there are no items in the queue, accessing the zero index results in an access error. It is easy to reproduce. Code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TStringDeque = specialize TDeque;var  Q: TStringDeque;  S: string;begin  Q := TStringDeque.Create;  Q.Free;end;causes an error. I would implement it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDeque.ClearData;var  i: SizeUint;begin  if FData = nil then    Exit;  if IsManagedType(T) then    Finalize(FData[Low(FData)], Length(FData));  FillChar(FData[Low(FData)], SizeUInt(Length(FData)) * SizeOf(T), 0);end;in document said, String is a managed type. but why it should Finalize explicitly? Fillchar does not finalize managed types. but T is string, then IsManagedType(T) hit, and then Finalize is running. that's what i met. ---------------------------------------------------------------------- Thaddy · 2024-03-14 10:50 That's why I quoted that code. Bart has provided a patch on the bug tracker. The rest fell asleep. (deep sleep) Thanks, Bart! And thanks Marco for committing it! ---------------------------------------------------------------------- ASerge · 2024-03-14 12:57 It's a pity that they didn't copy everything I suggested. Because the code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   if IsManagedType(T) then    Finalize(FData[Low(FData)], Length(FData));is smaller than Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   if IsManagedType(T) then    for i := Low(FData) to High(FData) do      Finalize(FData[i]); ---------------------------------------------------------------------- powerpcer · 2024-03-14 12:58 Quote from: Thaddy on March 14, 2024, 11:50:20 amThat's why I quoted that code. Bart has provided a patch on the bug tracker. The rest fell asleep. (deep sleep) Thanks, Bart! And thanks Marco for committing it! I don't know if Bart's solution works. but in my ARM MacOS, it raise exception on Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Finalize(FData[Low(FData)], Length(FData)); ---------------------------------------------------------------------- ASerge · 2024-03-14 12:59 Quote from: powerpcer on March 14, 2024, 01:58:13 pmI don't know if Bart's solution works. but in my ARM MacOS, it raise exception on Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Finalize(FData[Low(FData)], Length(FData)); Did you add Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  if FData = nil then    Exit;? ---------------------------------------------------------------------- powerpcer · 2024-03-14 13:05 Quote from: ASerge on March 14, 2024, 01:59:40 pmQuote from: powerpcer on March 14, 2024, 01:58:13 pmI don't know if Bart's solution works. but in my ARM MacOS, it raise exception on Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Finalize(FData[Low(FData)], Length(FData)); Did you add Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  if FData = nil then    Exit;? my question is , why ManagedType need to finalized explicitly? ---------------------------------------------------------------------- Thaddy · 2024-03-14 13:05 Simply look at the patch Marco has committed. That is different from Bart's. ( Why didn't you check the commit?) And, no, apart from very, very special cases (hackary) you do not need to finalize explicitly. ---------------------------------------------------------------------- ASerge · 2024-03-14 17:56 Quote from: powerpcer on March 14, 2024, 02:05:26 pmmy question is , why ManagedType need to finalized explicitly? When ordinary data is used, numbers, for example, are enough to zero them. But if managed types are used, such as strings, then after simply zeroing the references (via FillChar), the strings will remain in memory forever. The Finalize procedure checks that if it is the last reference, it additionally frees the string. ---------------------------------------------------------------------- Thaddy · 2024-03-15 05:37 And in normal cases you do not need to call it manually. ---------------------------------------------------------------------- powerpcer · 2024-03-15 13:50 Quote from: Thaddy on March 15, 2024, 06:37:43 amAnd in normal cases you do not need to call it manually. what is abnormal case? ---------------------------------------------------------------------- PascalDragon · 2024-03-18 21:05 Quote from: powerpcer on March 14, 2024, 02:05:26 pmmy question is , why ManagedType need to finalized explicitly? Because FillChar does not. If you simply write zeros over the FData array then you'll have a memory leak, because the the pointers to the data areas of the strings will be set to Nil and thus won't be freed anymore. Finalize ensures that these data areas are freed correctly before the pointers towards them are cleared. This is faster than manually doing a loop that sets each element to Default(T). ---------------------------------------------------------------------- marcov · 2024-03-19 11:31 Quote from: PascalDragon on March 18, 2024, 10:05:44 pmQuote from: powerpcer on March 14, 2024, 02:05:26 pmmy question is , why ManagedType need to finalized explicitly? Because FillChar does not. If you simply write zeros over the FData array then you'll have a memory leak, because the the pointers to the data areas of the strings will be set to Nil and thus won't be freed anymore. Finalize ensures that these data areas are freed correctly before the pointers towards them are cleared. Quote Are there loop versions of finalize for arrays ? ---------------------------------------------------------------------- ASerge · 2024-03-19 13:02 Quote from: marcov on March 19, 2024, 12:31:33 pmAre there loop versions of finalize for arrays ? Of course, see my example earlier: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Finalize(FData[Low(FData)], Length(FData));