#1 2017-08-03 06:44:45

mattsbg
Member
Registered: 2017-05-23
Posts: 20

Memory Leak in WinHttp Connection in Constructor

Hi,

I noticed that the construction of an TSQLDBWinHTTPConnectionProperties instance fails with an exception if the server is not available or the login information is incorrect.
As the exception happens in the constructor it leaks 504 bytes each time.
Overwriting the constructor and putting the inherited in a try except does not work either. Access violations happens on destroying the not fully created instance.

constructor TMyWinHTTPRemoteConnectionProperties.Create(const aServerName, aDatabaseName, aUserID, aPassWord: RawUTF8);
begin
  try
    inherited;
  except
    self.Free;
    raise;
  end;
end;

Also destroying a TSQLDBWinHTTPConnectionProperties instance where the server got unavailable results in an exception. I assume that also the memory of this instance won't be released.

Is this the expected behaviour?

thanks,
Matthias

Offline

#2 2017-08-03 07:53:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Memory Leak in WinHttp Connection in Constructor

You should never to this in any Delphi class.
The class destructor is called on any exception raised at constructor.
See _ClassCreate() function in System.pas RTL unit.

Offline

#3 2017-08-07 08:46:38

mattsbg
Member
Registered: 2017-05-23
Posts: 20

Re: Memory Leak in WinHttp Connection in Constructor

ok, granted. Didn't know that yet.

But nevertheless the exception in the constructor seems to cuase a memory leak with 504 bytes each time.

I used this code to measure the memory consumption before and after the object creation. I instanced a TSQLDBWinHTTPConnectionProperties with a unused server port.

function GetMemoryUsed: int64;
var
  ManagerState: TMemoryManagerState;
  BlockTypeState: TSmallBlockTypeState;
begin
  {$WARN SYMBOL_PLATFORM OFF}
  GetMemoryManagerState(ManagerState);
  {$WARN SYMBOL_PLATFORM ON}
  result := ManagerState.TotalAllocatedMediumBlockSize + ManagerState.TotalAllocatedLargeBlockSize;
  for BlockTypeState in ManagerState.SmallBlockTypeStates do
    result := result + BlockTypeState.UseableBlockSize * BlockTypeState.AllocatedBlockCount;
end;

Offline

Board footer

Powered by FluxBB