List – Delphi – Do I have to release all the elements in TOBJECT before release?

Please about this code:

type
TClient = class(TObject)
public
Host: String;
Queue: TIdThreadSafeStringList;
end;

var
Clients: TThreadList;

procedure TMain. FormCreate(Sender: TObject);
const
Hosts: Array[0..4] of String = (
'HOST1','HOST2','HOST3','HOST4,'HOST5 '
);
var
I: Integer;
List: TList;
Client: TClient;
begin
Clients := TThreadList. Create;
Clients.Duplicates := dupAccept;
for I := Low(Hosts) to High(Hosts) do
begin
Client := TClient.Create;
Client.Host := Hosts[I];
Client.Queue := TIdThreadSafeStringList.Create;
Clients.Add(Client);
Client := nil;
end;
end;

I want to know the correct way to release memory:

procedure TMain.FormDestroy(Sender: TObject);
var
I: Integer;
List: TList;
begin
List := Clients.LockLi st;
try
for I := 0 to List.Count-1 do
TClient(List[I]).Free;
finally
Clients.UnlockList;
Clients.Free;
end;
end;

Or like this:

procedure TMain.FormDestroy( Sender: TObject);
var
I: Integer;
List: TList;
begin
List := Clients.LockList;
try
for I := 0 to List.Count-1 do
begin
TClient(List[I]).Queue.Free;
TClient(List[I]).Free;
end;
finally
Clients.UnlockList;
Clients.Free;
end;
end;

In other words, I want I know when I release an object (TClient), do I automatically release all elements (queue), or I have to do it manually.

Thank you!

When the client object is destroyed, the queue object needs to be destroyed. However, the correct way is to let the client The end class is responsible for its members.

type
TClient = class
private
FHost: String;
FQueue: TIdThreadSafeStringList;
public
constructor Create(const Host: string);
destructor Destroy; override;
end;
....
constructor TClient. Create(const Host: string);
begin
inherited Create;
FQueue := TIdThreadSafeStringList.Create;
FHost := Host;
end;

destructor TClient.Destroy;
begin
FQueue.Free;
inherited;
end;

If you do, then it’s impossible Instantiate a class and cannot instantiate its members. Do it in your own way. Every time you need to instantiate the class, you must repeat the code to instantiate the members. It is easy to make mistakes in this way. More importantly, it makes the code more difficult Read and maintain.

Please about this code:

type
TClient = class (TObject)
public
Host: String;
Queue: TIdThreadSafeStringList;
end;

var
Clients: TThreadList;

procedure TMain.FormCreate(Sender: TObject);
const
Hosts: Array[0..4] of String = (
'HOST1','HOST2','HOST3','HOST4,'HOST5'
);
var
I: Integer;
List: TList;
Client: TClient;
begin
Clients := TThreadList.Create;
Clients.Duplicates := dupAccept;
for I := Low(Hosts) to High(Hosts) do
begin
Client := TClient.Create;
Client.Host := Hosts[I];
Client.Queue := TIdThreadSafeStringList.Create;
Clients.Add(Client);
Client := nil;
end;
end;

I want to know the correct way to release memory:

procedure TMain.FormDestroy(Sender: TObject);
var
I: Integer;
List: TList;
begin
List := Clients.LockList;
try
for I := 0 to List.Count-1 do
TClient(List[I]).Free;
finally
Clients.UnlockList;
Clients.Free;
end;
end;

Or like this:

procedure TMain.FormDestroy(Sender: TObject);
var
I: Int eger;
List: TList;
begin
List := Clients.LockList;
try
for I := 0 to List.Count-1 do
begin
TClient(List[I]).Queue.Free;
TClient(List[I]).Free;
end;
finally
Clients.UnlockList ;
Clients.Free;
end;
end;

In other words, I want to know that when I release an object (TClient), it automatically releases all elements (Queue), it must be executed manually.

Thank you!

When the client object is destroyed, the queue object needs to be destroyed. However, the correct way is to let the client class be responsible for its members.

type
TClient = class
private
FHost: String;
FQueue: TIdThreadSafeStringList;
public
constructor Create(const Host: string);
destructor Destroy; override;
end;
....
constructor TClient.Create(const Host: string);
begin
inherited Create;
FQueue := TIdThreadSafeStringList.Create;
FHost := Host;
end;

destructor TClient.Destroy;
begin
FQueue.Free;
inherited;
end;

If you do this, then it is impossible to instantiate the class and its members cannot be instantiated. Follow yourself Every time you need to instantiate a class, you must repeat the code to instantiate members. It is easy to make mistakes in this way. More importantly, it makes the code more difficult to read and maintain.

Leave a Comment

Your email address will not be published.