为什么程序在分配字符串后崩溃

-1 delphi string crash

我有整数和字符串的类,当我使用整数一切都很好,但当我使用字符串程序崩溃

//class
CatInfoType = class(TRemotable)
  private
    FcatId: Integer;
    FcatName: string;
    FcatParent: Integer;
    FcatPosition: Integer;
    FcatIsProductCatalogueEnabled: Integer;
  published
    property catId : Integer read FcatId write FcatId;
    property catName : string read FcatName write FcatName;
    property catParent : Integer read FcatParent write FcatParent;
    property catPosition : Integer read FcatPosition write FcatPosition;
    property catIsProductCatalogueEnabled: Integer 
                                       read FcatIsProductCatalogueEnabled 
                                       write FcatIsProductCatalogueEnabled;
end;
//program code 
procedure TForm2.Button7Click(Sender: TObject);
var
  rc: Integer;
  k: CatInfoType;
  l:String;
begin
  k.catId:=4646;
  k.catName:='777';//that crashing the program
end;
Run Code Online (Sandbox Code Playgroud)

J..*_*... 8

不......不太好

k.catId:=4646; //  <--- that crashing the program
k.catName:='777';
Run Code Online (Sandbox Code Playgroud)

您可能包含的错误消息就是这样的

在模块"MyProject.exe"中的地址xxxxxxxx处访问冲突.读取地址xxxxxxxx.

k是一类CatInfoType- 你还没有实例化它.你想要的是:

k := CatInfoType.Create;
k.catId := 4646;
//... etc
Run Code Online (Sandbox Code Playgroud)

  • 整数的赋值可能会成功,尽管它可能会破坏内存.字符串赋值不太可能成功,因为当前值将被最终确定.这可能是失败的原因. (2认同)