Delphi: Access Violation at the end of Create() constructor

匿名 (未验证) 提交于 2019-12-03 02:26:02

问题:

I have a very basic and simple class like this:

unit Loader;

interface  uses   Vcl.Dialogs;  type   TLoader = Class(TObject)   published       constructor Create();   end;  implementation  { TLoader }     constructor TLoader.Create; begin    ShowMessage('ok');  end;  end.

And from Form1 i call it like this:

procedure TForm1.Button1Click(Sender: TObject); var  the : TLoader; begin   the := the.Create; end;

Now, just after the the := the.Create part, delphi shows the message with 'ok' and then gives me an error and says Project Project1.exe raised exception class $C0000005 with message 'access violation at 0x0040559d: read of address 0xffffffe4'.

Also it shows this line:

constructor TLoader.Create; begin    ShowMessage('ok');  end; // 

I am new at delphi. I am using Delphi XE2 and i couldnt manage to fix this error. Does anyone show me a path or have solution for this?

回答1:

var   the : TLoader; begin   the := the.Create;

is incorrect. It should be

var   the : TLoader; begin   the := TLoader.Create;


回答2:

You've got the syntax wrong. If you're constructing a new object, you should use the class name, not the variable name, in the constructor call:

procedure TForm1.Button1Click(Sender: TObject); var  the : TLoader; begin   the := TLoader.Create; end;


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!