TLoginFrm中的BitBtn1属性Default:=False,ModalResult:=mrNone,Kind:=bkCustom
问题:我只要按一下BitBtn1后,程序就报错,报错提示:
Project zero.exe raised exception class EAccessViolation with message 'Access violation at address 4B49CD85. Read of address 4B49CD85'. Process stopped.Use Step or Run to continue.
真是奇怪,这个程序我几乎没有写什么代码,只给BitBtn1的Onclick事件中写了一句代码。但在程序运行中按下BitBtn1就报错。
在Project中Options选项里面我将MainForm和LoginFrm都移到Available Forms中了。
Auto-create forms中只有一个DataModule1.源代码如下:
program Zero;uses
  Forms,
  Controls,
  SysUtils,
  ZeroMain in 'ZeroMain.pas' {MainForm},
  ZeroLogin in 'ZeroLogin.pas' {LoginFrm},
  ZeroDM in 'ZeroDM.pas' {DataModule1: TDataModule};{$R *.res}
var
  LoginForm:TLoginFrm;
begin
  Application.Initialize;
  Application.CreateForm(TDataModule1, DataModule1);
  LoginForm:=TLoginFrm.Create(Application);
  if LoginForm.ShowModal=mrOk then
  begin
    FreeAndNil(LoginForm);
    Application.CreateForm(TMainForm,MainForm);
    Application.Run;
  end;
end.==========================
unit ZeroLogin;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls;type
  TLoginFrm = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Button1: TButton;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;implementation{$R *.dfm}procedure TLoginFrm.BitBtn1Click(Sender: TObject);
begin
  ModalResult:=mrCancel;     
end;
==============================
unit ZeroMain;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;type
  TMainForm = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  MainForm: TMainForm;implementation{$R *.dfm}end.以前我写过这种登录代码,没有遇到过错,我对照以前的代码,没有什么区别,但这次不知为何按BitBtn1这里总报错。
还请大侠们指点,谢谢!

解决方案 »

  1.   

    我后来试着去掉数据模块的建立,程序运行正常!
    奇怪呀,以前我有一个程序也是这样写的,没有出错,但这次为何会出错呢?
    编译里,在信息框中会有这样的提示:
    [Warning] ADODB.pas(2480): Comparison always evaluates to False
    [Warning] ADODB.pas(2480): Comparing signed and unsigned types - widened both operands我双击上面的提示,则位置跳到:
    function TParameters.CreateParameter(const Name: WideString;
      DataType: TDataType; Direction: TParameterDirection; Size: Integer;
      Value: OleVariant): TParameter;
    begin
      Result := AddParameter;
      Result.FParameter := Create_Parameter(Name, DataType, Direction, Size);
      { Don't try to assign value when it is an EmptyParam (used when optional) }
    [red]  if not ((TVarData(Value).VType = varError) and
        (TVarData(EmptyParam).VError = $80020004)) then [/red]
        Result.FParameter.Value := Value;
    end;我用单步执行时,出错的位置是:
    procedure TCustomForm.DestroyWindowHandle;
    begin
      if fsCreatedMDIChild in FFormState then
        SendMessage(Application.MainForm.ClientHandle, WM_MDIDESTROY, Handle, 0)
      else
        inherited DestroyWindowHandle;
    [red]  FClientHandle := 0;[/red]    //停在这条语句上。
    end;
      

  2.   

    真是奇怪了,我将数据模块的中ADOConnection,ADOQuery两个控件删除,程序也运行正常,难道这是ADO控件的BUG?还是我这样使用有问题?但我以前的程序中用ClientDataset不会出问题呀。 
      

  3.   

    我认为是程序上的问题,逻辑上不清楚
    var
      LoginForm:TLoginFrm;
    begin
      Application.Initialize;
      Application.CreateForm(TDataModule1, DataModule1);
      LoginForm:=TLoginFrm.Create(Application);
      try
        if LoginForm.ShowModal = mrcancel then
         Application.terminate;
      finally
        LoginForm.Free;
      end;  Application.CreateForm(TMainForm,MainForm);
      Application.Run;
    end.程序出错的原因好像是,application没有一个运行点,因为点击取消后,程序退出了。而你创建的LoginForm没有释放,