Skip to content
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

Cleanup guidance when working with COM #2123

Closed
vpopescu opened this issue Oct 27, 2022 · 2 comments
Closed

Cleanup guidance when working with COM #2123

vpopescu opened this issue Oct 27, 2022 · 2 comments
Labels
question Further information is requested

Comments

@vpopescu
Copy link

I hope this doesn't sound too generic.

Generally when working with COM in C++, there is very good guidance on what you need to do to clean up after yourself, depending on whether you are dealing with smart pointers or not. Take for example the following snippet from MS documentation:

        IWbemClassObject *pObj = 0;
        hRes = pEnum->Next(0,    1,   &pObj,   &uReturned);

         /// ...
        
        pObj->Release();    // Release objects not owned.  

Is there guidance on how to clean up COM references such as these, when using the crate? Do we just drop(pObj)? Or does the crate implement some type of smart pointers that are automatically cleaned up when they go out of scope? Looking at the documentation, it doesn't really imply I need to do any cleanup.

SImilarly, coming from C++, you are used to SysAllocString/SysFreeString to work with BSTR (unless you are using smart pointers), can I assume the rust implementation is a smart pointer, since there is no specific cleanup documented?

@vpopescu vpopescu changed the title Guidance on working with COM Cleanup guidance on working with COM Oct 27, 2022
@vpopescu vpopescu changed the title Cleanup guidance on working with COM Cleanup guidance when working with COM Oct 27, 2022
@tim-weis
Copy link
Contributor

COM interface implementations in the windows crate provide automatic resource management. While not immediately apparent from the documentation they transparently wrap an IUnknown struct, which has the Drop (and Clone) traits implemented, dealing with reference counting and releasing resources as needed.

Likewise, the BSTR struct provides both Drop and Clone implementations, so resource management is supplied out-of-the-box here as well.

This crate also has a large set of samples, such as com_uri and wmi, that illustrate proper use of the crate. The latter is also insightful in acknowledging that VARIANTs have not yet received resource management support.

@kennykerr kennykerr added the question Further information is requested label Oct 27, 2022
@vpopescu
Copy link
Author

vpopescu commented Oct 27, 2022

Thank you, very useful information, it may be good to add it to the FAQ discussed in another issue, if that will still happen (or maybe it's not a "frequently" asked question. I think SAFEARRAY may be in the same category as VARIANT, requiring SafeArrayDestroy()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants