为什么这里出现 SIGSEGV 异常

Why SIGSEGV Exception here

我正在尝试使用以下代码来创建一个简单的 GUI 应用程序:

program RnTFormclass;
{$mode objfpc}
uses
    {$IFDEF UNIX}{$IFDEF UseCThreads}
      cthreads,
    {$ENDIF}{$ENDIF}
    Interfaces, Forms, StdCtrls;

type
    RnTForm = class(TForm)
    private
        wnd: TForm;
        btn: TButton;
    public
        constructor create;
        procedure showit; 
    end; 

constructor RnTForm.create;
    begin
        Application.Initialize;
        wnd := TForm.Create(Application);
        with wnd do begin
            Height  := 300;
            Width   := 400;
            Position:= poDesktopCenter;
            Caption := 'LAZARUS WND';
            end;
        btn := TButton.Create(wnd);
            with btn do begin
            SetBounds(0, 0, 100, 50);
            Caption := 'Click Me';
            Parent  := wnd;
            end;
    end;

procedure RnTForm.showit;
    begin
        wnd.ShowModal; {Error at this line: Throws exception External: SIGSEGV }
    end;

var 
    myform1: RnTForm; 
begin
    myform1.create; 
    myform1.showit; 
end.

但是,它抛出异常,如上面代码中的注释所述。问题出在哪里,如何解决?

myform1.Create 应该是 myform1 := RnTForm.Create.

在你上面的代码中,myform1 是一个 nil 指针(因为它是一个全局变量,它被初始化为 nil)直到你给它赋值,在这个case a(指向 a 的指针)a RnTForm.

的新实例

当然,如果 myform1 是一个 nil 指针,你不能像它确实指向一个对象一样使用它(所以 myform1.showit 将不起作用)。