简体   繁体   English

表单显示上的 Delphi 访问冲突

[英]Delphi Access Violation on form show

Sorry for having to open new question but I can't find an answer anywhere.很抱歉不得不提出新问题,但我在任何地方都找不到答案。

My app is still in progress, but basically I'm trying to call another Form from my MainForm when initializing players, however I get an Access Violation error.我的应用程序仍在进行中,但基本上我试图在初始化播放器时从我的 MainForm 调用另一个 Form,但是我收到了 Access Violation 错误。 Would you please explain to me why this could be happening?你能解释一下为什么会发生这种情况吗?

My MainForm code:我的主窗体代码:

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, jpeg, pngimage, getPlayer_u;...

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  Randomize;

  InitGameSetup();
end;...

procedure TfrmMain.InitGameSetup();
begin
  SetWindowProperties();
  InitBackGround();
  InitMainMenu();
  InitGameBoard();
  InitScrabbleTileRack();
  InitPlayers();
//  GameLoop();
end; ...

procedure TfrmMain.InitPlayers();
var
  I : Integer;
  sName, sSurname : string;
begin
  setLength(Players, NUMBER_OF_PLAYERS);
  for I := 1 to High(Players) do
    begin
      GetPlayer(); ---------------- problem is here
      with Players[I] do
        begin
          Name := sName;
          Surname := sSurname;
        end;
    end;
end;...

procedure TfrmMain.GetPlayer();
begin
  frmGetPlayer.Show;
end;

My frmGetPlayer :我的frmGetPlayer

unit getPlayer_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TfrmGetPlayer = class(TForm)
    btnSubmit: TButton;
    edtName: TEdit;
    edtSurname: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure btnSubmitClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    sPlayerName, sPlayerSurname : string;
  end;

var
  frmGetPlayer: TfrmGetPlayer;

implementation

{$R *.dfm}

procedure TfrmGetPlayer.btnSubmitClick(Sender: TObject);
begin
  sPlayerName := edtName.Text;
  sPlayerSurname := edtSurname.Text;

  if not ((Length(sPlayerName) >= 1) and (Length(sPlayerSurname) >= 1)) then
    MessageDlg('Please enter a name and surname.', mtInformation, [mbOK], 0)
  else
    Self.Free;
end;

procedure TfrmGetPlayer.FormCreate(Sender: TObject);
begin
  with Self do
    begin
      Position := poScreenCenter;
      BorderStyle := bsDialog;
    end;
end;

end.

My dpr:我的 dpr:

program main_p;

uses
  Forms,
  main_u in 'main_u.pas' {frmMain},
  getPlayer_u in 'getPlayer_u.pas' {frmGetPlayer};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TfrmMain, frmMain);
  Application.Run;
end.

The error:错误:

错误信息:访问冲突

Only your MainForm object is created automatically at program startup.只有您的 MainForm 对象在程序启动时自动创建。 Inside its OnCreate event, your Player Form object hasn't been created yet, so the frmGetPlayer variable is not pointing at a valid object.在其OnCreate事件中,您的 Player Form 对象尚未创建,因此frmGetPlayer变量未指向有效对象。

frmGetPlayer is a global variable, so it is initially nil . frmGetPlayer是一个全局变量,所以它最初是nil The error message is telling you that you are accessing invalid memory near address 0, which is almost always an indication of accessing a member of a class via a nil pointer.错误消息告诉您,您正在访问地址 0 附近的无效内存,这几乎总是表示通过nil指针访问类的成员。

So, you can't call frmGetPlayer.Show() until after you have created the Player Form object and assigned frmGetPlayer to point at it.因此,在创建 Player Form 对象并指定frmGetPlayer指向它之前,您不能调用frmGetPlayer.Show() Which the code you showed is not doing.你展示的代码没有做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM