Windows: Please refrain from calling CoInitalize(Ex) in new code

17 views
Skip to first unread message

Aaron Klotz

unread,
Jun 15, 2021, 2:26:06 PM6/15/21
to dev-pl...@mozilla.org
(bcc: firefox-dev)

Hi everyone,

Now that bug 1707954[1] has merged into central, I'd like to ask that
developers landing patches involving Windows COM objects do *not* add
any new calls to CoInitialize, CoInitializeEx, or OleInitialize (or
their (Co|Ole)Uninitialize counterparts).

I have several reasons for asking this:

* We are already initializing COM on a process-wide basis. For several
years now, we have been configuring COM for Gecko's main thread during
early startup via the mozilla::mscom::ProcessRuntime class. Now that bug
1707954 has merged, ProcessRuntime also configures COM for background
threads such that they are always implicitly initialized into COM's
multi-threaded apartment. We also use ProcessRuntime for COM
initialization in our content processes and in other specialized child
processes as necessary.

* CoInitialize(Ex) calls can be expensive. It is more performant to do
it once than to book-end every single region of COM code with these
APIs, calling them over and over.

* The documentation and sample code for these APIs is somewhat
misleading. Developers frequently interpret calling `CoInitialize()` or
`CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED)` as meaning,
"initialize COM and tell it that I don't want to worry about multiple
threads," when actually there are significant consequences and side
effects of initializing COM in this manner. I would argue that the
single-threaded apartment model should be used if and only if the thread
uses a Win32 message pump. ProcessRuntime always does the Right Thing,
taking into account the current process's e10s type and sandbox
configuration.

I have filed a new bug[2] to add a linter for this, but until that is
ready, I would appreciate everybody's cooperation!

If you have any further questions or concerns about this, please reach
out to me and we can discuss them.

Thanks!

Aaron



[1] https://bugzilla.mozilla.org/show_bug.cgi?id=1707954
[2] https://bugzilla.mozilla.org/show_bug.cgi?id=1715650

--
You received this message because you are subscribed to the Google Groups "firef...@mozilla.org" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firefox-dev...@mozilla.org.
To view this discussion on the web visit https://groups.google.com/a/mozilla.org/d/msgid/firefox-dev/9da38df8-d48e-dd71-4b02-95fda47d5dee%40mozilla.com.
Reply all
Reply to author
Forward
0 new messages