Why is this crashing? lazarus / 31-LCL ---------------------------------------------------------------------- kupferstecher · 2021-04-15 17:07 Hello, attached a minimum project, with a derived class (TPanel). When I override the procedure Resize and try to access a child control in that procedure then the application crashes on startup. Seems the components Resize procedure is called before even the components Constructor was finished. What I am doing wrong? Is this intended behaviour? The derived class is created in the Forms OnCreate handler: 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.FormCreate(Sender:TObject);begin  LabelPanel:= TLabelPanel.Create(self);  LabelPanel.Parent:= self;  LabelPanel.Align:= alClient;end; And the derived class: 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 TLabelPanel = class(TPanel)    BackgndShape: TShape;  public    Constructor Create(TheOwner:TComponent);override;    Destructor Destroy;override;    Procedure Resize;override; end; IMPLEMENTATION Constructor TLabelPanel.Create(TheOwner:TComponent);begin  inherited Create(TheOwner);   BackgndShape:= TShape.Create(Self);  BackgndShape.Parent:= Self;  BackgndShape.Brush.Color:= clGreen;  BackgndShape.Pen.Width:= 10;  BackgndShape.Left:= 0;  BackgndShape.Top:= 0; end; Procedure TLabelPanel.Resize;begin  inherited Resize;   //if assigned(BackgndShape) then begin   //No chrash if checked for assignment first    BackgndShape.Left:= 10;    BackgndShape.Top:= 10;    BackgndShape.Width:= Width-20;    BackgndShape.Height:= Height-20;  //end; end;  EDIT: Lazarus 2.0.10 and 2.0.12 on Win64 ---------------------------------------------------------------------- Martin_fr · 2021-04-15 17:16 Well, without testing, I would say: Move the TLabelPanel  "inherited create" call to the end of the constructor. As for Resize being called, no idea if that is "by design". But, even if there is no handle, during "create" the bounds are initialized (there is some func to deliver default values). So if the bounds are set, then Resize would be called. When you create a form from an LFM a lot of other stuff happens. You need to check the TReader and related classes. Afaik (but that is from very sketchy memory) the class creation is split into Memory alloc, and constructor. So the order may be different. Also cslLoading will be set (afaik even while the constructor runs / or maybe in the parent/owner?), which again can affect behaviour. If interested dive into the source yourself.... ---------------------------------------------------------------------- Martin_fr · 2021-04-15 17:23 Right so, this is what I meant (LFM reading) fpc_3.2.2\source\rtl\objpas\classes\reader.inc line 945 .... TReader.ReadComponent 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           NewComponent := TComponent(ComponentClass.NewInstance); // Get Memory            if ffInline in Flags then              NewComponent.FComponentState :=                NewComponent.FComponentState + [csLoading, csInline];            NewComponent.Create(Owner);    // Constructor (yes, called on the instance)  Of course TReader may be subclassed, and more behavioural changes may exist. ---------------------------------------------------------------------- kupferstecher · 2021-04-15 18:22 Thanks for the fast respond! Quote from: Martin_fr on April 15, 2021, 07:16:05 pmWell, without testing, I would say: Move the TLabelPanel  "inherited create" call to the end of the constructor.I just checked, that doesn't work either, when setting the Parent of the child (i.e. self), then the application crashes, because the component isn't properly initialized. The place where Resize is called I also found. In inherited Create the default bounds are set, there also Resize is called. 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 (not (csLoading in ComponentState)) then  begin    Resize;    DebugInvalidPos(5);    CheckOnChangeBounds;    DebugInvalidPos(6);    // for delphi compatibility send size/move messages    if PosSizeChanged then      SendMoveSizeMessages(SizeChanged,PosChanged);  end; Seems like csLoading is not set. Imho Resize actually should be called, probably its just not possible to do that after "inherited" (in an automatic manner). So I guess it is like it should be. Not ideal though. ---------------------------------------------------------------------- Martin_fr · 2021-04-15 19:42 Quote from: kupferstecher on April 15, 2021, 08:22:29 pmI just checked, that doesn't work either, when setting the Parent of the child (i.e. self), then the application crashes, because the component isn't properly initialized. If the owner (i.e Tchild.Create(self)) works, then you can move the  ".Parent" assignment after the initialized. Of course that breaks the code into small bits... And I actually do not know if "Create(Self)" is allowed, before "Self" is fully created. (Because it needs the list for all the owned components / Though if that list is created on request / if needed, then it should be ok). The other way is to check in "Resize". QuoteSeems like csLoading is not set. It is only set, if someone sets it. TReader does set it. You can call Self.Loading to set it. And then Self.Loaded to clear it. But if you register your component, and it gets created from LFM, then you may want to check, if it was set before. So you don't interfere with TReader. Because TReader may defer it until other components have also been loaded. So components can fix up all their relationships. Not sure, if the docs have anything on the topic.... ---------------------------------------------------------------------- Martin_fr · 2021-04-15 19:55 Also for some reason TReader is not always setting it (or not always at the same time). Depends on inlining, which IIRC is e.g. used by frames.... But I do not recall all details.... ---------------------------------------------------------------------- MarkMLl · 2021-04-15 22:20 Quote from: Martin_fr on April 15, 2021, 09:55:43 pmAlso for some reason TReader is not always setting it (or not always at the same time). Depends on inlining, which IIRC is e.g. used by frames.... But I do not recall all details.... Aaah... I've been meaning to ask for some while why Lazarus/LCL frames required a certain minimum compiler version. (Assume you've just answered that question, or at least provided a broad hint). MarkMLl ---------------------------------------------------------------------- Martin_fr · 2021-04-15 22:32 How old a compiler version? And does not the rest of the IDE need an up todate compiler too (unless you use an old IDE)? ---------------------------------------------------------------------- MarkMLl · 2021-04-16 06:34 Quote from: Martin_fr on April 16, 2021, 12:32:55 amHow old a compiler version? And does not the rest of the IDE need an up todate compiler too (unless you use an old IDE)? Assuming that's for me: the IDE tends to have a bit of slop in compiler tolerance, but my recollection is that when frames were introduced they required that it be built with a specific version of the compiler because of some detail of code generation... it was somewhere around 2.6.4. I never asked why and didn't experiment. MarkMLl ---------------------------------------------------------------------- kupferstecher · 2021-04-16 09:10 Quote from: Martin_fr on April 15, 2021, 09:42:23 pmIf the owner (i.e Tchild.Create(self)) works, then you can move the  ".Parent" assignment after the initialized. Of course that breaks the code into small bits... I just tried that, it works. Yes, as you said, its not ideal for compact code. Also the 'self' thing I don't like. For such things I always fear that something gets broken in deeper levels without a direct crash. Thats also why I opened this topic. But seems at least one can relay on the crash because the children are automatically initialized with nil. What should be possible regarding 'self', is to use the owner of the Form (TheOwner passed in Create) instead of 'self' and then deleting the Child manually in Destroy instead of relying on the Owner. QuoteQuoteSeems like csLoading is not set. It is only set, if someone sets it. TReader does set it. You can call Self.Loading to set it. And then Self.Loaded to clear it. Understood, the Application actually has no chance to set csLoading, as I create the control on runtime. Even if it would be set while the "FormCreate" handler is called, it would still be crashing in cases, the control is created in any other circumstance. So I understand it is how it is :-) ---------------------------------------------------------------------- Martin_fr · 2021-04-16 10:38 Quote from: kupferstecher on April 16, 2021, 11:10:30 amAlso the 'self' thing I don't like. For such things I always fear that something gets broken in deeper levels without a direct crash. Thats also why I opened this topic. But seems at least one can relay on the crash because the children are automatically initialized with nil. What should be possible regarding 'self', is to use the owner of the Form (TheOwner passed in Create) instead of 'self' and then deleting the Child manually in Destroy instead of relying on the Owner. It is perfectly legal to do 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 LabelPanel:= TLabelPanel.Create(Owner);  or IIRC even 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 LabelPanel:= TLabelPanel.Create(nil);  In the first case, you can destroy it if you do so before the owner gets destroyed. But if you get destroyed be the owner, then LabelPanel may have already gone. If you destroy it, that will remove it from the owners list. So you might better use 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 TLabelPanel.FreeNotification(self); // and back you are with selfand in the current class, that holds the reference 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 Notification(AComponent: TComponent; Operation: TOperation); override;Which allows you to nil TLabelPanel, when it gets destroyed. In any case you do need to set the parent. (which might be self, or might have a reference to self) ---------------------------------------------------------------------- kupferstecher · 2021-04-16 17:42 Quote from: Martin_fr on April 16, 2021, 12:38:22 pmIn the first case, you can destroy it if you do so before the owner gets destroyed. But if you get destroyed be the owner, then LabelPanel may have already gone. Right, I missed that. Thank you!