-
Notifications
You must be signed in to change notification settings - Fork 503
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
Clarification of the lifecycle of COM implementations #3173
Comments
COM interfaces, implemented by the Here's a deep dive on how this works: https://www.pluralsight.com/courses/com-essentials |
Thanks @kennykerr, I may well end up taking that course to bootstrap my COM knowledge for this current project. Your explanation makes sense. Whenever you have a spare moment I'm curious on your thoughts regarding the Understanding Threading Issues states. It's not entirely clear, but that at least suggests the possibility that if I pass a It also states which might imply (given that MTA is assumed for this API) that such event handlers can be called in parallel. Would determining the fact of these matters and enforcing them be within the scope of your work here? Or are these facets of the Rust <--> COM programming model considered to be within the domain of the API caller (me)? |
I think I addressed that part of your question here: #3171 (comment) |
Perhaps you did and I'm just too ignorant to see how. As I see it currently, this question is slightly different. To summarize and maybe clarify: when I call a function like My question relates to the case where I want to create my own custom #[implement(IUIAutomationFocusChangedEventHandler)]
pub struct AutomationFocusChangedEventHandler; I'm arguing that, given the nature of |
Suggestion
I'm building a custom COM event handler using the
implement
macro, however I'm finding myself lost regarding the lifetime/ownership of said implementation. It's easiest to explain by referring to an exampleExample
Cargo.toml
"Where" is the
Box<dyn Fn(&IUIAutomationElement)>
?The crux of the program is that I create this custom
IUIAutomationFocusChangedEventHandler
object (handler
), pass a reference toAddFocusChangedEventHandler(&handler)
, and thendrop(handler)
.My initial instinct is to expect this program to crash whenever the focus changes -- I've
drop
ped thehandler
, and it is ostensibly the owner of theBox<dyn Fn(&IUIAutomationElement)>
that is ultimately called by the event callback. If that memory has been dropped, then I should crash when the event handler tries to call it. However instead the program functions fine.I believe what may be happening is that when I convert the custom
AutomationFocusChangedEventHandler
into()
anIUIAutomationFocusChangedEventHandler
, ownership of thatBox<dyn Fn(&IUIAutomationElement)>
is taken byIUIAutomationFocusChangedEventHandler
, and ultimately managed for me by the generated code, which in turn manages it via COM reference counting. However I was unable to confirm this one way or another; theimplement
macro is difficult to parse, and I'm not sure precisely howinto()
is being implemented.Thread safety
Another thought I have is that in the case of this particular event API, the
handler
can be called from another thread, and might be called concurrently/in-parallel (e.g. if two focus changes happen in quick succession, and the second triggers the handler before the first has had a chance to finish executing). Therefore in rust terms, my customought to be
Send + Sync
.Granted this API is
unsafe
, and so the responsibility is on me to ensure that I use it safely given it's underlying semantics, however I'd like to try and confirm my understanding here (relates to #3171).May relate to #3036
The text was updated successfully, but these errors were encountered: