Subject: Leaked handles on Windows

Leaked handles on Windows

From: Vlad Dinulescu <vlad.dinulescu_at_avira.com>
Date: Wed, 13 May 2009 10:05:53 +0300

Hello,

We had troubles with leaked handles using the c-ares library, on
Windows. After almost two weeks of digging, we found out that the
problem lies within ares_init_options(), when calling
GetNetworkParams(). The call of this function leaves a single open
handle which is never destroyed until the process terminates. Every
further call to ares_init_options leaves another open handle. If one
opens lots of ares channels, lots of handles are being leaked. After
commenting out the call to GetNetworkParams(), and letting c-ares find
the nameserver information from registry keys, everything went well,
without any leaked handles. We're running Windows XP SP2 and Windows XP
SP3 (same behaviour).
        We also found this thread dating from 2005, which describes the same
problem:
        http://www.ureader.com/msg/1474561.aspx

        Are you aware of this problem?
        What would the best solution (or better said alternative) be?

        I thought of:
a) making Microsoft release a service pack for GetNetworkParams() and
forcing all users worldwide install it – not doable.

b) commenting out the call to GetNetworkParams and letting c-ares find
the nameserver information from registry keys. But I am not sure if
c-ares finds the nameserver information on all versions of Windows. What
about Vista? Vista 64? Windows 7 ?
        This is the simplest alternative I could come up with, but it would
need lots of testing.

c) letting c-ares leak a handle , and trying to open as few channels as
possible (reuse them). This would imply a lot of coding and testing in
our application, however.

d) maybe something else – in c-ares, trying to get the nameserver
information from registry keys first and only if that fails, calling
GetNetworkParams().

e) letting the user set the DNS server information through a callback.
This would let the user solve the problem :)

        I am looking forward to hearing from you!

Best regards,
Vlad Dinulescu
Received on 2009-05-13