Delphi: TList 创建,内存错误
Delphi: TList Creation, memory error
我尝试创建 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 [=12=]8BACCF. Exception class $C0000005 with message 'access violation at 0x008baccf: read of address 0x00000074'. Process Server.exe (5824)
我不知道我做错了什么:\
编辑
对我来说有点乱...
但是我有 3 个文件,ipname 在上面,第二个是一个名为 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;
我真的不知道我应该在哪里调用构造函数所以我在创建表单时调用了...
题中的代码没问题。错误的代码是调用构造函数的代码。我敢打赌它是这样写的:
var
Data: IpNameData;
....
Data.Create;
应在何处阅读:
Data := IpNameData.Create;
根据大卫的回复+评论,你应该更换
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;
请随时给大卫 "Checkmark" 回复 - 我只是详细说明了他的回复。
我尝试创建 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 [=12=]8BACCF. Exception class $C0000005 with message 'access violation at 0x008baccf: read of address 0x00000074'. Process Server.exe (5824)
我不知道我做错了什么:\
编辑
对我来说有点乱...
但是我有 3 个文件,ipname 在上面,第二个是一个名为 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;
我真的不知道我应该在哪里调用构造函数所以我在创建表单时调用了...
题中的代码没问题。错误的代码是调用构造函数的代码。我敢打赌它是这样写的:
var
Data: IpNameData;
....
Data.Create;
应在何处阅读:
Data := IpNameData.Create;
根据大卫的回复+评论,你应该更换
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;
请随时给大卫 "Checkmark" 回复 - 我只是详细说明了他的回复。