On Wed Jul 30 08:13:21 2025 +0000, Rémi Bernon wrote:
As closing/freeing the `HDEVQUERY` is performed asynchronously, I have
also added `IWeakReference` support for the device watcher. If this is only meant to fix some tests I don't think it's worth the added complexity. We only care about matching Windows (weak) refcounting when necessary: if an application depends on it, or if it is required to break reference cycle.
We need weakref support as the HDEVQUERY object is created with async close events. That allows us to not manually create a Stopped event in order to implement `Stop`. However, this also means that if the watcher gets freed without being stopped first, the HDEVQUERY gets closed in Release. As DevCloseObjectQuery asynchronously frees the query object, using weak references allows us to implement Release without first waiting for the DevQueryStateChange event before freeing the device watcher.