Forum > General

[SOLVED] SIGSEGV on free pointer Tframe

(1/2) > >>

superc:
Hello,
I want to create dinamically tframe on  tpanel and free it for loading another; I use this code for create TFrame:


--- 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 TMonitorForm.Button1Click(Sender: TObject);var  hwnd: TFrame;begin  hwnd := CreateFrameAndHostPanel(pnlRight, pnlRight, pnlRight);  pntFrame := @hwnd;  ShowMessage(IntToStr(TFrameLEDMonitor(pntFrame^).getNumber));   TFrameLEDMonitor(pntFrame^).Free;         end;   function TMonitorForm.CreateFrameAndHostPanel(_Owner: TComponent; _Parent: TWinControl; _Panel: TPanel): TFrame;begin  Result := FrameLcd.TFrameLCDMonitor.Create(Owner);  Try    Result.Parent := _Parent;  Except    FreeAndNil(Result);    raise;  End;end;     
In this code Free works well and free pointed frame, but if I move free line of code in another button like as:

--- 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 TMonitorForm.Button2Click(Sender: TObject);begin  TFrameLEDMonitor(pntFrame^).Free;end;       
I get and SIGSEGV error and access violation; pntFrame is declared in public section and is typed as:


--- 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";}};} ---type  pointerTFrame = ^TFrame;                                        
I don't understand why,

thanks in advance.

marcov:
You specified an owner when creating the frame. An owner (I assume the form) frees the owned class (the frame) on shutdown.

IIRC you can remove the owner registration using TComponent.RemoveComponent

superc:
Excuse Marcov, I don't understand; i must remove owner as default passed with RemoveComponent?

Thanks

howardpc:
Try declaring your public instance reference simply as

--- 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";}};} ---tempFrame: TFrameLEDMonitor;and drop the local variable from your click event handler:
--- 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 TMonitorForm.Button1Click(Sender: TObject);begin  tempFrame := CreateFrameAndHostPanel(...);  ShowMessage(...);  tempFrame.Free;  tempFrame := Nil;end; 

superc:

--- Quote from: howardpc on May 12, 2021, 10:38:33 am ---Try declaring your public instance reference simply as

--- 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";}};} ---tempFrame: TFrameLEDMonitor;and drop the local variable from your click event handler:
--- 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 TMonitorForm.Button1Click(Sender: TObject);begin  tempFrame := CreateFrameAndHostPanel(...);  ShowMessage(...);  tempFrame.Free;  tempFrame := Nil;end; 
--- End quote ---

Works fine with

--- 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";}};} ---  public    { public declarations }    pntFrame: TFrame;       
I've 5 Frame different, Thank you very much...






Navigation

[0] Message Index

[#] Next page

Go to full version