FreeAndNil revisited, but now properly. lazarus / 21-General ---------------------------------------------------------------------- Thaddy · 2024-07-01 16:19 FreeAndNil has many dislikes, mainly because of its signature: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 FreeAndNil(var Obj);So the procedure is typeless, you can call it on anything. What you want is a typed FreeAndNil that can only accept TObject descendants. Well a post by Jeroen Pluimers a couple of days ago brought me new insights on how it could be done. It takes a bit of cheating, but it does not affect it has of being more type safe. What if we change the signature to: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 FreeAndNil(constref Value:TObject);Now of course you are not be able to change a constref, but by using a trick that I have been using for many years, that is quite possible. That trick may even be a contribution to the insight that the Delphi developers had, because I showed them. (in a slightly different context, I must say). You can implement a typesafe FreeAndNil 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 FreeAndNil(constref value:TObject);var  tmp:TObject;begin  tmp:=value;  // This is using MY - very old - trick!! but in a different context.  TObject(Pointer(@Value)^) := nil;  Tmp.Free;end;So now you have a typesafe FreeAndNil that only can accept TObject descendants.  Should we do it like that too? Literature: 1: https://wiert.me/2024/06/26/delphi-has-had-a-more-type-safe- freeandnil-or-a-while-now-but-in-order-to-do-so-it-lies-to-you/ 2. https://dalijap.blogspot.com/2020/06/magic-behind-freeandnil.html I was really one if not the first that pulled that trick (in Delphi and Lazarus and PowerBASIC), btw. You may have seen it in my step code, but used it elsewhere. In the comments on 2) there is even a better solution (using generics) that is not possible in Delphi but is possible in FPC. I will add that later. [edit][and edit some more, Mark's request]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 {$mode delphi}{$modeswitch implicitfunctionspecialization}uses classes;procedure FreeAndNil(var Value:T);var  Tmp:T;begin  Tmp:=Value;  Value := nil;  Tmp.Free;end; var  a:Tstringlist;begin  a := TStringlist.Create;  { implicit function specialization }  FreeAndNil(a);end. This can only be done in trunk... And not in Delphi. And is completely typesafe. ---------------------------------------------------------------------- MarkMLl · 2024-07-01 16:53 Interesting. Will that work- or is there a variation which could work- on things like TStringList.Objects[ i] ? I'm currently working on code that assembles a multi-line message into a stringlist, parses it into properties of an attached object, and then either transfers the object into a cache or copies the content of the object into the cache (depending on what's in the cache already). I've been struggling with finding a problem which turned out to be a fairly obvious double-free (obvious, that is, once I was looking in the right place :-) It's not multithreaded, but things like that do tend to focus ones mind when thinking about indivisibility and critical sections. MarkMLl ---------------------------------------------------------------------- Thaddy · 2024-07-01 16:59 Implemented that. See above. Thanks. Removed the hack completely too. If the objects are owned, they will be free'd too. That is to say that freeing the objects must go to the same FreeAndNil. That might need a small implementation change if that is not the case. ---------------------------------------------------------------------- Martin_fr · 2024-07-01 17:06 Of course there is one problem. I don't know if it is possible in todays Fpc, but it can come into existing at any time. 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 FreeAndNil(constref Value:TObject); begin {...} end; begin // Foo is a LOCAL var  foo := GetFoo; // some actual instance   if Foo <> nil then    FreeAndNil(Foo);  if Foo <> nil then    Foo.Bar;end; The "constref" Tells the compiler that the the pointer in "Foo" is not going to change. Therefore the compiler could optimize that code, to assume that the 2nd "if" condition is still true. ---------------------------------------------------------------------- Thaddy · 2024-07-01 17:11 I removed the hack completely in an edit2. Please examine that code, Martin. ---------------------------------------------------------------------- MarkMLl · 2024-07-01 18:06 Quote from: Thaddy on July 01, 2024, 06:59:21 pmImplemented that. See above. Thanks. Removed the hack completely too. If the objects are owned, they will be free'd too. That is to say that freeing the objects must go to the same FreeAndNil. That might need a small implementation change if that is not the case. Being able to handle properties properly is a definite advance, and for that many thanks :-) MarkMLl ---------------------------------------------------------------------- Martin_fr · 2024-07-01 18:27 Yes, using a generic will work fine. ---------------------------------------------------------------------- Thaddy · 2024-07-01 20:39 After generics are fixed, that is. At the moment you can not declare it in a separate unit interface and implementation section. There is a known bug in generics that need to be fixed for that. But it works fine in the implementation section or in short programs. ---------------------------------------------------------------------- BrunoK · 2024-07-01 21:12 A non problem looking for a complicated solution. ---------------------------------------------------------------------- jamie · 2024-07-01 21:59 Quote from: BrunoK on July 01, 2024, 11:12:23 pmA non problem looking for a complicated solution. Exactly. ---------------------------------------------------------------------- Thaddy · 2024-07-02 11:35 It is absolutely not a non-problem. FreeAndNil has many disadvantages and this solves one of them: type safety. The other one that needs to be solved is scope. If FreeAndNil is not called in the same scope as where the class is created you run into many problems, one of which is that you can't find double free' s in your code if you over-use it. Current FreeAndNil is an anti-pattern in the way it is used. It hides bugs, and does not solve them. There are very few places in code where FreeAndNil should be called instead of just Free. ---------------------------------------------------------------------- Martin_fr · 2024-07-02 13:36 Quote from: Thaddy on July 02, 2024, 01:35:32 pm one of which is that you can't find double free' s in your code if you over-use it. How does FreeAndNil affect "double free" ? That is, if we clarify the terminology. - "Free" in "double free" refers to un-allocation (destroy/freemem) - "Free" in "FreeAndNil" refers to TObject.Free => which means "conditional destroy: "If obj <> nil then obj.Destroy" "Conditional destroy" is not (in itself) a double free, even (or especially) if it is called with nil. Calling   FreeAndNil(nil);   TObject(nil).Free; is valide, and not a double free (as in destroy). It does check for that nil. A double free, is only when and if a variable still has a dangling pointer, to memory/instance that was un-allocated. That can happen with a 2nd variable, and it does not matter if the first was un-allocated with "destroy" or "FreeAndNil". You may argue, that with a single (local) variable, the code should not need the "var := nil" because the code should know anyway, and should not access that dangling pointer. So maybe FreeAndNil is over-used, but in most cases that does not hurt. As for the cases where the code does not know (Instance created in a condition), you could argue that the "destroy" should then use the same condition, rather than checking for nil. Though, if the variables in the condition may have changed when the decision is needed, then the result of the initial condition would need to have been stored in some flag-variable. And that could well be the "nil" in the object var. Equally there is valid code, where a flag is needed that a condition was met, and the object had been destroyed. If such code is written, then FreeAndNil is perfectly fine. ---------------------------------------------------------------------- Thaddy · 2024-07-02 13:46 It is very simple to explain. If you reference an object that is already free'd again with a Free (the same pointer) that means for debugging purposes you can locate the code that has free'd it first AND the code that tried to free it a second time, usually from a different scope. If you use freeandnil, there is no way to debug such code, because you are cheated into a false believe the code is OK. IOW there is a dangling pointer in your code, but you chose the easy way out and you will never know why there is a dangling pointer and where. You can't back-trace to where it is actually wrong. Although such code (unnecessary nilled) works and does not leak it masks out a serious problem that can be avoided in your design. Hence I almost always - not always - call just free. But that has less to do with the fact that the current signature is not type safe. That only helps while debugging if freeandnil is called on an unexpected class or basically anything. ---------------------------------------------------------------------- runewalsh · 2024-07-02 13:50 It was rejected in https://gitlab.com/freepascal.org/fpc/source/-/issues/40277. You can use trunk SysUtils.Exchange 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 {$modeswitch implicitfunctionspecialization}Exchange(obj, nil).Free; which is more wordy, but safer than both var untyped and constref and not limited to Free. ---------------------------------------------------------------------- Thaddy · 2024-07-02 13:58 There is no constref..... My last approach is different because I do not cheat: the reason why it was rejected is no longer valid. It is 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 FreeAndNil(var Value:T);var  Tmp:T;begin  Tmp:=Value;  Value := nil;  Tmp.Free;end;In combination with implicitfunctionspecialization. But as I mentioned: there is still an outstanding bug in generics - not in the above code - that needs to be resolved before this is anything near to production. The code works as in the example I gave you, but as yet, you can not put the signature in a published section, because the compiler refuses that and that is a known bug. Not mine. That is the same bug that my last Auto() exposed and Sven answered to that. ---------------------------------------------------------------------- korba812 · 2024-07-02 14:06 Quote from: Thaddy on July 02, 2024, 03:46:42 pmIt is very simple to explain. If you reference an object that is already free'd again with a Free (the same pointer) that means for debugging purposes you can locate the code that has free'd it first AND the code that tried to free it a second time, usually from a different scope. If you use freeandnil, there is no way to debug such code, because you are cheated into a false believe the code is OK. IOW there is a dangling pointer in your code, but you chose the easy way out and you will never know why there is a dangling pointer and where. You can't back-trace to where it is actually wrong. Although such code (unnecessary nilled) works and does not leak it masks out a serious problem that can be avoided in your design. Hence I almost always - not always - call just free. But that has less to do with the fact that the current signature is not type safe. That only helps while debugging if freeandnil is called on an unexpected class or basically anything. It's true. But on the other hand, it can hide errors like "use after free". 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 program Project1; type  TFoo = class    Bar: Integer;    procedure DoFooBar(AValue: Integer);  end; procedure TFoo.DoFooBar(AValue: Integer);begin  Bar := AValue;end; var  F: TFoo; begin  F := TFoo.Create;  F.DoFooBar(10);  F.Free;  F.DoFooBar(20);  Writeln(F.Bar);end. ---------------------------------------------------------------------- Martin_fr · 2024-07-02 14:10 Quote from: Thaddy on July 02, 2024, 03:46:42 pmIt is very simple to explain. If you reference an object that is already free'd again with a Free (the same pointer) that means for debugging purposes you can locate the code that has free'd it first AND the code that tried to free it a second time, usually from a different scope. If you use freeandnil, there is no way to debug such code, because you are cheated into a false believe the code is OK. IOW there is a dangling pointer in your code, but you chose the easy way out and you will never know why there is a dangling pointer and where. You can't back-trace to where it is actually wrong. Although such code (unnecessary nilled) works and does not leak it masks out a serious problem that can be avoided in your design. Hence I almost always - not always - call just free. But that has less to do with the fact that the current signature is not type safe. That only helps while debugging if freeandnil is called on an unexpected class or basically anything. So you would rather we all wrote code 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 var   MyObject: TFoo;  IsAllocatedMyObject: Boolean;begin....  MyObject := TFoo.Create;  IsAllocatedMyObject := True;...begin  if IsAllocatedMyObject then begin    MyObject.Free;    IsAllocatedMyObject := False;  end; ---------------------------------------------------------------------- Thaddy · 2024-07-02 14:39 That won't be necessary... because you would write better code, because you can actually debug it... In its current state FreeAndNil is nay useless and encourages people to write bad code. ---------------------------------------------------------------------- Martin_fr · 2024-07-02 15:19 Quote from: Thaddy on July 02, 2024, 04:39:03 pmThat won't be necessary... There is code that would become way to complex if it needed to be "designed" to avoid this. That was the point, I was trying to make. And for that, FreeAndNil is a nice and useful helper. But, if entertaining the design goal you propose, then it is not (only) "FreeAndNil". It is also "Free", because you only should need "Destroy". "Free" just adds a test for "nil", but if you never set "nil" then you don't need to test either. ---------------------------------------------------------------------- Thaddy · 2024-07-02 17:29 Quote from: Martin_fr on July 02, 2024, 05:19:48 pmThere is code that would become way to complex if it needed to be "designed" to avoid this. My ethics are different. If I would have to transfer that - "way to complex" code - over to a colleague when I left the company it proved I did a bad job. If code is "way too complex", it should be analyzed. Such code should not exist. I would also never get grumpy over extra time in my teams. (and some of those team members are on this forum) That is also called management. At any level. ---------------------------------------------------------------------- dbannon · 2024-07-03 00:34 Thaddy,  I have been feeling guilty about using FreeAndNil() for years. All because Thaddy said it is evil.  Now we find its  because I might call FreeAndNil(Pointer) where Pointer points to (eg) Int ? If I am writing code as bad as that, I have a lot more to worry about than FreeAndNil(). Davo ---------------------------------------------------------------------- jamie · 2024-07-03 01:15 Quote from: dbannon on July 03, 2024, 02:34:09 amThaddy,  I have been feeling guilty about using FreeAndNil() for years. All because Thaddy said it is evil.  Now we find its  because I might call FreeAndNil(Pointer) where Pointer points to (eg) Int ? If I am writing code as bad as that, I have a lot more to worry about than FreeAndNil(). Davo LOL good one