Solved

Quick check on dynamic array deprecation and any resulting memory leaks

Posted on 2008-06-20
4
281 Views
Last Modified: 2010-04-05
A little information on how this program works:

Main winsock component waits for new connections.
When a new connection arrives it takes the dynamic  array holding all the connections and sets the length to one larger.(I.E. SetLength(ServerClients,TotalConnections + 1)). In the new space allocolated it creates a new winsock object to take the connections.(Shown below).

Now when the connection is closed I FreeAndNil() that spot in the dynamic array. So you may have (con1,con2,nil,con4). What I have done(also shown below) is first take the nil out of the array by moving the next one in the array down. You would then have (con1, con2, con4, con4). I then reset all tags and information to reorder the array to (con1, con2, con3, con4). Now that con4 isn't necessary I set the length down one.

Now I thought I would have to put a FreeAndNil() for the con4 but that just gives me errors. It works without it but I wonder if I'm leaking memory this way.

Any suggestions on how this could be done differently? Or if it's fine as it is?
procedure TForm1.Winsock1ConnectionRequest(ASender: TObject;
  requestID: Integer);
var
  CurrentClient : TClientWinsock;
begin
  Inc(TotalClients);
  SetLength(ServerClients,TotalClients);
  ServerClients[TotalClients - 1] := TClientWinsock.Create(Winsock1);
  CurrentClient := ServerClients[TotalClients - 1];
  CurrentClient.LocalPort := LocalPort;
  CurrentClient.Tag := TotalClients - 1;
  CurrentClient.Ping := 0;
  CurrentClient.IdleTime := 0;
  CurrentClient.CurrentUser := '';
  CurrentClient.ActiveWindow := '';
  CurrentClient.OnDataArrival := WinsockDataArrival;
  CurrentClient.OnClose := WinsockClose;
  CurrentClient.OnError := WinsockError;
  CurrentClient.Close;
  CurrentClient.Accept(requestID);
end;
 
 
procedure TForm1.WinsockClose(Sender: TObject);
var
  I,J,Max : Integer;
  CurrentClient : TClientWinsock;
begin
  CurrentClient := (Sender as TClientWinsock);
  CurrentClient.Close;
  J := CurrentClient.Tag;
  ComboBox1.Items.Clear;
  FreeAndNil(ServerClients[J]);
  Dec(TotalClients);
  For I := J To High(ServerClients) Do
    begin
    If I = High(ServerClients) Then
      begin
      SetLength(ServerClients,TotalClients);
      Break;
    end;
    ServerClients[I] := ServerClients[I + 1];
    //FreeAndNil(ServerClients[I + 1]);
    ServerClients[I].Tag := ServerClients[I].Tag - 1;
  end;
  For I := Low(ServerClients) To High(ServerClients) Do
    begin
    ComboBox1.Items.AddObject(ServerClients[I].RemoteHostIP,TObject(ServerClients[I].Tag));
  end;
  Panel2.Caption := '';
  Panel3.Caption := '';
  Panel4.Caption := '';
  Panel5.Caption := '';
  Panel6.Caption := '';
end;

Open in new window

0
Comment
Question by:ThievingSix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 10

Assisted Solution

by:dinilud
dinilud earned 50 total points
ID: 21836400
i think , there is no memory leak.

you can check this with 'EurekaLog' or 'FastMM'
0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 50 total points
ID: 21836573
why don't you use a TCollection with a descendant of TCollectionItem ?
0
 
LVL 2

Accepted Solution

by:
Ustin19 earned 400 total points
ID: 21837195
dinilud is on point: it is no memory leaks
(con1, con2, con3, con4) - is pointers to objects, when (con1, con2, con4, con4) reduces to (con1, con2, con4) pointer to con4 stores in element [2] and all right.
Error appears when you freeandnil con4 (!) at first step of loop and then use it's method ServerClients[I].Tag at next iteration :)
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 21837262
saves all the hassle with using a collection
no need to reorder
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question