Leak Madness

delphi package - automated exception handling

Leak Madness

Postby FredS » Sat Feb 04, 2017 11:58 pm

Hi,
I've tried all I can think of to stop a Leak Report from popping up but no luck.
I narrowed it down to these two functions:
WinVerifyTrust
CryptQueryObject

I am going to use WinVerifyTrust because its simpler:
Code: Select all
class function TSigningCert.VerifyTrust(const Filename: string): Boolean;
var
  file_info: TWinTrustFileInfo;
  trust_data: TWinTrustData;
begin
  FillChar(file_info, SizeOf(file_info), 0);
  file_info.cbStruct := sizeof(file_info);
  file_info.pcwszFilePath := PChar(Filename);

  FillChar(trust_data, SizeOf(trust_data), 0);
  trust_data.cbStruct := sizeof(trust_data);
  trust_data.dwUIChoice := WTD_UI_NONE;
  trust_data.fdwRevocationChecks := WTD_REVOKE_WHOLECHAIN;//instead of WTD_REVOKE_NONE
  trust_data.dwProvFlags := WTD_REVOCATION_CHECK_CHAIN;
  trust_data.dwUnionChoice := WTD_CHOICE_FILE;
  trust_data.InfoUnion.pFile := @file_info;

  If not WinVerifyTrust(INVALID_HANDLE_VALUE, WINTRUST_ACTION_GENERIC_VERIFY_V2, @trust_data) then Exit(False);
  Result := True;
end;


That gives me this Leak Report:

allocation number: 5439
program up time: 4.04 s
type: Thread Handle
handle: $a9c
access rights: $1fffff
threadId: $1124
processId: $2748
process exe: D:\My Projects\DFX\Lib\Build_All\Win32\Build_All_Lib_Files.exe

main thread ($17fc):
671cd7f2 madExcept32.dll madExceptDbg 3805 CreateThreadCallback
004aca9b Build_All_Lib_Files.exe madExcept HookedCreateThread
75a2f0c8 wintrust.dll SoftpubLoadMessage
75a2de54 wintrust.dll WinVerifyTrust
00847c46 Build_All_Lib_Files.exe SigningCertifDfx 310 TSigningCert.VerifyTrust
00984cdd Build_All_Lib_Files.exe Build_All_Lib_Files 175 initialization
757062c2 KERNEL32.DLL BaseThreadInitThunk


I've even wrapped the whole thing in StopLeakChecking and it still comes up:
Code: Select all
   madExcept.StopLeakChecking(True);
     {TSigningCert}
     with TSigningCert.create(FileName, true) do
     try
       if VerifyTrust.Error then TDlg.Show('VerifyTrust Failed');
     finally
       Free;
     end;
   madExcept.StartLeakChecking(True);
FredS
 
Posts: 79
Joined: Mon May 11, 2015 9:42 pm

Re: Leak Madness

Postby madshi » Wed Feb 15, 2017 1:30 pm

It seems weird that StopLeakChecking doesn't seem to be effective. Can you reproduce this in a small test project? If so, I can have a look at it.

Alternatively to using StopLeakChecking you could also use "HideLeak()" providing a part of the callstack. Without having tested it, maybe "HideLeak('WinVerifyTrust')" might work?
madshi
Site Admin
 
Posts: 9268
Joined: Sun Mar 21, 2004 5:25 pm

Re: Leak Madness

Postby FredS » Tue Feb 21, 2017 9:24 pm

Sure, occasionally test projects actually re-create the issue :D

HideLeak('WinVerifyTrust') works.
Attachments
MAD StopLeakChecking.zip
Small test project zipped, contains mbr file
(147.71 KiB) Downloaded 15 times
FredS
 
Posts: 79
Joined: Mon May 11, 2015 9:42 pm

Re: Leak Madness

Postby madshi » Wed Mar 08, 2017 2:17 pm

Thank you for the test project!

After some debugging and code checking I found that the Start/StopLeakChecking APIs are actually meant to first start and then stop leak checking, in case you don't have leak checking activated globally. These APIs are not meant to be used to temporarily disable leak checking. Basically StopLeakChecking has the one and only purpose of undoing a prior StartLeakChecking call.

I suppose adding some sort of "PauseLeakChecking" API to temporarily disable leak checking might be a useful API, but it doesn't exist right now. But the HideLeak() call works for you, so you don't need any changes atm, right?
madshi
Site Admin
 
Posts: 9268
Joined: Sun Mar 21, 2004 5:25 pm

Re: Leak Madness

Postby FredS » Thu Mar 09, 2017 6:53 pm

madshi wrote:Thank you for the test project!
I suppose adding some sort of "PauseLeakChecking" API to temporarily disable leak checking might be a useful API, but it doesn't exist right now. But the HideLeak() call works for you, so you don't need any changes atm, right?


Yes it works, but "PauseLeakChecking" would be nice for the future. What should have taken a minute took multiples of that. I know the function doesn't actually leak and in an app that runs longer than a few seconds no leak is reported when Leak Checking is active.

So the one Minute solution would have been just to ignore that section of code which I tried with StopLeakChecking. The longer solution required me to find out which call(s) caused it, add the madexcept unit to this unit, and add both functions to HideLeak().
FredS
 
Posts: 79
Joined: Mon May 11, 2015 9:42 pm


Return to madExcept

Who is online

Users browsing this forum: Google [Bot] and 3 guests