lnet
lnet copied to clipboard
Clearing memory on an error when trying to do TLUDP.Listen
trafficstars
#2?
program Project1;
uses lNet;
var Server, Client:TLConnection;
const Port=17001; //any
begin
Server:=TLUdp.Create(nil);
WriteLn(Server.Listen(Port)); //True
Client:=TLUdp.Create(nil);
WriteLn(Client.Listen(Port)); //False
Client.Free;
Server.Free;
end.
Console:
148 memory blocks allocated : 79739/80192
147 memory blocks freed : 79467/79920
1 unfreed memory blocks : 272
True heap size : 622592
True free heap : 622112
Should be : 622128
Call trace for block $00007FFFF7F3B2E0 size 272
$000000000042C85A LISTEN, line 1211 of ../program/components/lnet/lib/lnet.pp
$000000000040037F main, line 15 of project1.lpr
My fix: lnet.pp
procedure TLUdp.Disconnect(const Forced: Boolean = False);
begin
if Assigned(FRootSock) then begin
FRootSock.Disconnect(True); // true on UDP it always goes there anyways
FRootSock := nil; // even if the old one exists, eventer takes care of it
end;
end;
To
procedure TLUdp.Disconnect(const Forced: Boolean = False);
begin
if Assigned(FRootSock) then begin
FRootSock.Disconnect(True); // true on UDP it always goes there anyways
FreeAndNil(FRootSock); // even if the old one exists, eventer takes care of it
end;
end;
Sorry but your solution is not always true:
if disconnect is called within a socket event then FRootSock is not allowed to be freed (if done so the eventer will crash when tries to free the FRootSock) if Disconnect is called outside a socket event then FRootSock needs to be freed otherwise a memory leack is created.
=> So there is the need to figure out which is the case, or a way to tell the eventer that it is not allowed to free the FRootSock anymore as it is already freed.