Delphi:TList创建,内存错误

时间:2015-04-27 10:35:07

标签: delphi tlist

我试图创建3个TList但是,当我创建它们时,我得到了错误。

unit ipname;

interface

uses
  System.Generics.Collections;

type
  IpNameData = class
  private
    ips : TList<String>;
    ports : TList<integer>;
    names : TList<String>;
  public
    procedure Add(ip:string; port:integer; name:string);
    procedure Del(ip:string; port:integer; name:string);
    procedure Clear();
    function GetName(i:integer):string;
    function GetIp(i:integer):string;
    function GetPort(i:integer):integer;
    function GetSize(i:integer):integer;


  published
    constructor Create;
  end;
implementation

constructor IpNameData.Create;
begin
  ips := TList<String>.Create();
  ports := TList<integer>.Create();
  names := TList<String>.Create();
end;
...

所以问题发生在程序到达ips := TList<String>.Create();时,我得到:

First chance exception at $008BACCF. Exception class $C0000005 with message 'access violation at 0x008baccf: read of address 0x00000074'. Process Server.exe (5824)

我无法弄清楚我做错了什么:\

修改

对我来说它有点乱......

但是我有3个文件,ipname是upper,第二个是名为ServerData的DataModule

unit ServerData;

interface

uses
   System.SysUtils, System.Classes, IdBaseComponent, IdComponent,
    IdCustomTCPServer, IdTCPServer, IdContext,FMX.Dialogs, ipname;
type
  TDataModule2 = class(TDataModule)
    TCPServer: TIdTCPServer;
    procedure TCPServerConnect(AContext: TIdContext);
    procedure TCPServerDisconnect(AContext: TIdContext);
    procedure TCPServerExecute(AContext: TIdContext);
  private
    { Private declarations }
     procedure CMDToDo(cmd,ip:string; p:integer);
  public
    { Public declarations }
    Logs:TStrings;
    IPs:TStrings;
    Data : IpNameData;
    procedure SendMSG(IP,msg : string; port : integer);
  end;

var
  DataModule2: TDataModule2;

最后一个是ServerGUI

unit ServerGUI;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
  FMX.Memo, FMX.StdCtrls, FMX.Controls.Presentation, FMX.Edit, FMX.ListBox;

type
  TForm1 = class(TForm)
    Options: TGroupBox;
    Power: TSwitch;
    PortField: TEdit;
    Port_label: TLabel;
    Power_label: TLabel;
    LogM: TMemo;
    Logs: TGroupBox;
    IPBox: TListBox;
    IPBox_label: TLabel;
    LogM_label: TLabel;
    procedure PowerSwitch(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

uses ServerData;
procedure TForm1.FormCreate(Sender: TObject);
begin
   ServerData.DataModule2.Data.Create;
end;

我真的不知道我应该在哪里调用构造函数,所以我在创建表单时调用...

2 个答案:

答案 0 :(得分:3)

问题中的代码很好。错误的代码是调用构造函数的代码。我准备打赌它写着:

var
  Data: IpNameData;
....
Data.Create;

应该在哪里阅读:

Data := IpNameData.Create;

答案 1 :(得分:0)

根据David的回复+评论,你应该替换

procedure TForm1.FormCreate(Sender: TObject);
begin
   ServerData.DataModule2.Data.Create;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   ServerData.DataModule2.Data:=IpNameData.Create;
end;

并更新

IpNameData = class
  private
    ips : TList<String>;
    ports : TList<integer>;
    names : TList<String>;

IpNameData = class
  private
    ips : TList<String>;
    ports : TList<integer>;
    names : TList<String>;
    destructor Destroy; override;

并添加

destructor IpNameData.Destroy;
begin
  ips.Free;
  ports.Free;
  Name.Free;
  inherited Destroy;
end;

并更改

constructor IpNameData.Create;
begin
  ips := TList<String>.Create();
  ports := TList<integer>.Create();
  names := TList<String>.Create();
end;

constructor IpNameData.Create;
begin
  inherited Create;
  ips := TList<String>.Create();
  ports := TList<integer>.Create();
  names := TList<String>.Create();
end;

随意给David回复“Checkmark” - 我只是详细说明了他的回复。