Page 1 of 1

Leak Madness

Posted: Sat Feb 04, 2017 11:58 pm
by FredS
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);

Re: Leak Madness

Posted: Wed Feb 15, 2017 1:30 pm
by madshi
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?

Re: Leak Madness

Posted: Tue Feb 21, 2017 9:24 pm
by FredS
Sure, occasionally test projects actually re-create the issue :D
HideLeak('WinVerifyTrust') works.

Re: Leak Madness

Posted: Wed Mar 08, 2017 2:17 pm
by madshi
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?

Re: Leak Madness

Posted: Thu Mar 09, 2017 6:53 pm
by FredS
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().

Re: Leak Madness

Posted: Sat Jul 14, 2018 1:54 pm
by MFr
Push! I need this function too.

With HideLeak() i can avoid creating a class as been reported as leakes. But if this class reserves memory (as a example TThread) this memory will be reported.

Re: Leak Madness

Posted: Wed Jul 18, 2018 8:18 am
by madshi
I'll add this to my to do list, but it might not make it into madExcept 4.x, but may have to wait for 5.x.