-
Notifications
You must be signed in to change notification settings - Fork 513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for freeing handles automatically #3013
Conversation
This looks like really nice stuff! Is the metadata enriched to know when functions like |
This isn't very clear today. Hopefully in future... |
As it happens, another update to the `windows` family of crates is available: https://github.com/microsoft/windows-rs/releases/tag/0.57.0 In particular, this one includes a new feature to support freeing handles automatically: microsoft/windows-rs#3013 This avoids the need for the `OwnedHandle` helper type in Sudo.
The Win32 metadata includes information about how to free certain kinds of handles. Since ownership is often unknown or contextual, this is not used to implement the standard
Drop
trait. Instead, a newFree
trait provided by thewindows-core
crate is implemented for all handles with appropriate metadata. Thefree
function can be used to free the handle manually, but anOwned<T>
wrapper is provided to callfree
automatically when the owned handle is dropped.This is supported by nearly 150 different handle types - not just
HANDLE
- including various crypto handle types, registry, GDI and USER32 types, and many more.Workarounds are provided for the following Win32 metadata bugs:
RAIIFree
impliesInvalidHandleValue
win32metadata#1891RAIIFree
functions have wrong type win32metadata#1892Here's an example.
Before:
After:
The BCrypt test provides a more comprehensive example: e386c51
Previously, the test was just leaking. You can see how
Owned
supports all kinds of BCrypt handle types through type inference.Related: #2638 #2468 #2222