Improving Assigned function lazarus / 21-General ---------------------------------------------------------------------- egsuh · 2021-01-02 05:38 I have been frustrated frequently by untraceable SEGSIV error, but I found one of hundreds of my mistakes I have not noticed until now.  If I write as follows: 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    a, b: TMyClass;begin    a:= TMyClass.Create;     b:= a;    FreeAndNil(a);      if Assigned(b) then b.Free;   // This seems to cause SEGSIV error. b is not NILed. end;      This looks quite stupid when listed line by line, but when one class is used in many units, it's not easy to trace the creation and destruction of objects. Isn't there any way to improve Assigned function?  Or, what about using b as pointer to a? I cannot imagine whole structure but there might be someway here. ---------------------------------------------------------------------- lucamar · 2021-01-02 09:22 Assigned can only test easily whether a "pointer" (impicit or explicit) is Nil or not, thus it fails in constructions like the one you cite, since b is *not* Nil Only way I can think of for it to be better would be to make it test whether the memory manager has that memory assigned to something, but that's not an easy task. But if I may be so bold, I'd suggest that tracing the creation and destruction of objects is indeed the responsability of the programmer. It is not always easy but if one keeps rather strictly to sane principles of programming (abstraction, single responsability, etc.) it doesn't become the nightmare you seem to think. ---------------------------------------------------------------------- PascalDragon · 2021-01-02 09:31 Quote from: egsuh on January 02, 2021, 06:38:16 amIsn't there any way to improve Assigned function?  Or, what about using b as pointer to a? I cannot imagine whole structure but there might be someway here. There is no way to catch this (especially not for Assigned). If you copy the pointer to the class instance it's your responsibility to ensure that it's handled correctly. ---------------------------------------------------------------------- Handoko · 2021-01-02 10:36 This code below works. But maybe not what OP want.  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 TForm1.Button1Click(Sender: TObject);var    a, b: TButton;begin    a:= TButton.Create(nil);    b:= a;    FreeAndNil(a);    if Assigned(b) and (b = a) then      b.Free;end; ---------------------------------------------------------------------- egsuh · 2021-01-02 10:45 I'm thinking following construction...  this is not likely to be implemented, but anyway: - constructing an object creates another intermediate variables.        A:= TObject.Create;          ==>       AnObj := TObject.Create;   // AnOBJ directly points to the object itself       A := ^AnObj;        And         A.Free;           ==>         AnObj.Free;         AnObj:= nil;          In this case, B:= A; will be equivalent to  B:=  A^; So once an object is freed, all variables pointing to it (pointing to internal variable AnObj) will all return nil value.  I know there is a serious flaw in my logic for now.  "A" should return AnObj^ when doing A.DoAnything or A.Free,  and ^AnObj at B:= A; . But can't we think of "pointer to pointer" type, which returns first or second variable as suitable? ---------------------------------------------------------------------- PascalDragon · 2021-01-02 11:05 Quote from: egsuh on January 02, 2021, 11:45:12 amI'm thinking following construction...  this is not likely to be implemented, but anyway: Correct, a change like that won't be done, so there isn't any use in thinking about it. ---------------------------------------------------------------------- egsuh · 2021-01-02 23:21 I have given some time to think over this. This is exactly what property does. The problem is that property is defined only within the object. But as Free Pascal allows global property, so this might be possible. ---------------------------------------------------------------------- egsuh · 2021-01-02 23:58 I wrote a quick test program. Following codes work fine.  I cannot write like AClass.Free nor FreeAndNil(AClass).  So I twisted a little bit so that assigning nil frees the target object. 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   TBaseClass=class      F1, F2: string;      procedure setFields(v1, v2: string); virtual; abstract;   end; var   ABaseClass: TBaseClass = nil; function getBaseClass: TBaseClass;procedure setBaseClass(b: TBaseClass); property AClass: TBaseClass read getBaseClass write setBaseClass;property BClass: TBaseClass read getBaseClass write setBaseClass;property CClass: TBaseClass read getBaseClass write setBaseClass; implementation function getBaseClass: TBaseClass;begin   Result:= ABaseClass;end; procedure setBaseClass(b: TBaseClass);begin    if b = nil       then FreeAndNil(ABaseClass)       else ABaseClass:= b;end;  procedure TForm1.Button2Click(Sender: TObject);begin   AClass:= TBaseClass.Create;   AClass.setFields('Field 1', 'Field 2');    Showmessage (ABaseClass.F1);   BClass := nil;   if CClass=nil then showmessage('You have Freed successfully');    BClass:= TBaseClass.Create;   FreeAndNil(ABaseClass);   if AClass=nil then showmessage('You have Freed successfully again');end;  But I cannot assign as followings. Neither of following properties work. 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 a: integer;property VarA: integer read a write a;property AClass:TBaseClass read ABaseClass; So, on the restriction that only the original variable can free the object, this works much like object properties.  Hallelujah Free Pascal and Lazarus !!! ---------------------------------------------------------------------- Warfley · 2021-01-03 01:01 Try to think of your classes in ownership principles. Every class instance you create should have exactly one owner. This owner takes care of freeing the class. This owner can be a function or another object. Every other access to that instance "borrows" the access from your owner. They can use the object as long as the owner keeps it alive, but don't need to care about freeing it. If you create multiple objects of the same type that needs to be freed simultaniously, TObjectList is your friend, this is a list that automatically frees instances when they get deleted from the list. If you have sinlge instances (like your TObjectList that holds multiple objects), your owner must free it explicetly. If your owner is a class, it is most often usefull to free the instances in that classes destructor. If it is a function, usually try-finally is used to ensure the freeing. By using COM interfaces you could also make use of reference counting, which can be usefull, but you have to restrict yourself to using interfaces. If management operators will be fixed, one could also create a reference counted "smart pointer" with a managed record which will take care of the memory management all on it's own, so you don't need to call free anymore. Also "unique pointers" could be implemented that way, which can be used to enforce ownership semantics and add less overhead than smart pointers. (I already have an implementation for both, but until the bug is fixed they are currently unusable) ---------------------------------------------------------------------- egsuh · 2021-01-03 06:08 Yes. You are generally right. The reason I'm thinking of using property or similar thing is that multiple forms within one application need to access one object which is also a member of a larger object. The object is not necessary always. Some forms need it, while others not. This requires to check whether the object is created or not whenever to try to access it. If not created yet, it can be created within the form.  And sometimes it is needed to free it, and re-create within a form. So I'm thinking about something like property, so that I can free and create at any point. Yes.. I'm thinking about TObjectList, which will delete all the objects at the last moment.  And also thinking creating all objects at the beginning, even though they will be never used. Or I can trace the programs flow very carefully ---- this is the right answer, and I do this when most jobs are done. But during the development, I'd like to see what I'm doing will work eventually. So objects are created and freed at many places. So, just looking for ways of doing things more easily before the last clean-up. ---------------------------------------------------------------------- Handoko · 2021-01-03 06:21 Quote from: egsuh on January 03, 2021, 07:08:07 am... multiple forms within one application need to access one object which is also a member of a larger object. The object is not necessary always. Some forms need it, while others not. This requires to check whether the object is created or not whenever to try to access it. If not created yet, it can be created within the form.  And sometimes it is needed to free it, and re-create within a form. I can understand why you need it. But I think the way you work can lead to hard to maintain code. If the object needs to be used by several forms, why don't you just write it on a shared unit and set it as a global variable? So there won't be multiple variables point to an object. I know some programmers against to use of global variables. You can write a function "CreateSomethingIfNil" to access the object, which set local to the unit so it is not public to the outside. I use such trick in some of my code. ---------------------------------------------------------------------- Thaddy · 2021-01-03 06:43 Interfaces!(COM style) ---------------------------------------------------------------------- Warfley · 2021-01-03 14:21 Quote from: Handoko on January 03, 2021, 07:21:53 amI know some programmers against to use of global variables. You can write a function "CreateSomethingIfNil" to access the object, which set local to the unit so it is not public to the outside. I use such trick in some of my code. Don't call it a global variable but "singleton pattern" and suddenly this isn't a bad practice anymore, but a best practice and "enterprise coding" Quote from: Thaddy on January 03, 2021, 07:43:34 amInterfaces!(COM style) Exactly, your baseclass needs to inherit from TInterfacedObject and this automatically implements refcounting and you don't have to care for the freeing at all anymore ---------------------------------------------------------------------- egsuh · 2021-01-03 16:21 Quote But I think the way you work can lead to hard to maintain code.Yes, you are right. What I want is to make whole application running, so that I can figure out all the requirements on the common object. Not easy to figure out them in advance. QuoteInterfaces!(COM style)I'll have a look at it..... but I do not want to study anymore^^. Anyway thank you for all the comments. ---------------------------------------------------------------------- jamie · 2021-01-03 16:59 Maybe what you should be doing is making a Reference to the OBJECT A instead of copying the pointer.. so  PMyClass = TMyClass; Var  P:PMyClass; begin   P := @SomeInstanceSomeWhere; …. Later on...  if P^ <> Nil Then.... or FreeAndNil(P^); something like that I suppose