Closed Bug 123683 Opened 23 years ago Closed 12 years ago

CoInitialize should be called at most once per thread

Categories

(Core :: XPCOM, defect)

x86
Windows 2000
defect
Not set
normal

Tracking

()

RESOLVED INCOMPLETE
mozilla1.2alpha

People

(Reporter: dougt, Unassigned)

Details

CoInitialize is an expensive call which only need to occur once in any
appication.  We should remove all of these calles and have main() make this call.

/embedding/browser/activex/src/pluginhostctrl/nsURLDataCallback.cpp, line 137 --
HRESULT hr = CoInitialize(NULL);
/embedding/browser/activex/tests/cbrowse/cbrowse.cpp, line 149 -- HRESULT hRes =
CoInitializeEx(NULL, COINIT_MULTITHREADED);
/embedding/browser/activex/tests/cbrowse/cbrowse.cpp, line 151 -- HRESULT hRes =
CoInitialize(NULL);
/intl/chardet/src/windows/nsNativeDetectors.cpp, line 131 -- HRESULT hr =
CoInitialize(NULL);
/intl/chardet/src/windows/nsNativeDetectors.cpp, line 222 -- HRESULT hr =
CoInitialize(NULL);
/xpcom/io/nsFileSpecOS2.cpp, line 331 -- CoInitialize(NULL);
/xpcom/io/nsFileSpecOS2.cpp, line 385 -- CoInitialize(NULL);
/xpcom/io/nsFileSpecWin.cpp, line 292 -- CoInitialize(NULL);
/xpcom/io/nsFileSpecWin.cpp, line 342 -- CoInitialize(NULL);
/xpcom/io/nsLocalFileOS2.cpp, line 337 -- CoInitialize(NULL); // FIX: we should
probably move somewhere higher up during startup
/xpcom/io/nsLocalFileWin.cpp, line 100 -- CoInitialize(NULL); // FIX: we should
probably move somewhere higher up during startup
/xpinstall/src/nsWinShortcut.cpp, line 51 -- CoInitialize(NULL);
/xpinstall/wizard/windows/setup/shortcut.cpp, line 51 -- CoInitialize(NULL);
/xpinstall/wizard/os2/setup/shortcut.cpp, line 52 -- CoInitialize(NULL);
/gfx/src/windows/nsDrawingSurfaceWin.cpp, line 851 -- CoInitialize(NULL);
/webshell/tests/viewer/nsViewerApp.cpp, line 1628 -- CoInitialize(NULL);
/widget/src/windows/nsToolkit.cpp, line 154 -- ::CoInitialize(NULL);
/mailnews/mapi/mapiDll/MapiDll.cpp, line 83 -- HRESULT hRes =
::CoInitialize(nsnull) ;
/mailnews/mapi/mapihook/src/msgMapiSupport.cpp, line 142 -- ::CoInitialize(nsnull) ;
At least a few of these:
/embedding/browser/activex/src/pluginhostctrl/nsURLDataCallback.cpp, line 137 
-- HRESULT hr = CoInitialize(NULL);
/embedding/browser/activex/tests/cbrowse/cbrowse.cpp, line 149 -- HRESULT hRes 
= CoInitializeEx(NULL, COINIT_MULTITHREADED);
/embedding/browser/activex/tests/cbrowse/cbrowse.cpp, line 151 -- HRESULT hRes 
= CoInitialize(NULL);
/xpinstall/src/nsWinShortcut.cpp, line 51 -- CoInitialize(NULL);
/xpinstall/wizard/windows/setup/shortcut.cpp, line 51 -- CoInitialize(NULL);
/xpinstall/wizard/os2/setup/shortcut.cpp, line 52 -- CoInitialize(NULL);
/webshell/tests/viewer/nsViewerApp.cpp, line 1628 -- CoInitialize(NULL);
/mailnews/mapi/mapiDll/MapiDll.cpp, line 83 -- HRESULT hRes =
::CoInitialize(nsnull) ;
/mailnews/mapi/mapihook/src/msgMapiSupport.cpp, line 142 -- 
::CoInitialize(nsnull) ;

Are for standalone apps.  If you remove their call as you attempt to move to 
'main' you'll probably break them.
yup.  we need to sort though these call sites and find the required ones.
this is low hanging perf fruit.  Hopefully we can get to it for 1.0.
Target Milestone: --- → mozilla1.0
bill, is this something you can own?
Assignee: dougt → law
Err, OK.  I think I know where to put the call (nsNativeAppSupportWin::Start). 
I might parcel out the work of deciding which of those call sites can remove
their call; I don't know much about any of them.
Here is the deal.  the Windows xpcom local file code need to make sure that
CoInitialize has been called.  I do not want this call removed from xpcom as
some developer embedding mozilla might forget to do that.  However, there are
other places, above XPCOM, which do not need to make this call.
Moving Netscape owned 0.9.9 and 1.0 bugs that don't have an nsbeta1, nsbeta1+,
topembed, topembed+, Mozilla0.9.9+ or Mozilla1.0+ keyword.  Please send any
questions or feedback about this to adt@netscape.com.  You can search for
"Moving bugs not scheduled for a project" to quickly delete this bugmail.
Target Milestone: mozilla1.0 → mozilla1.2
i'd propose zapping the intl and gfx references and probably one of the 
mailnews references.
The summary is not right
CoInitialize() should be called once per *thread*
So, if you have several threads ...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/cmf_a2c_36qt.asp
Component: Browser-General → XPCOM
Summary: CoInitialize should only be called once per application → CoInitialize should only be called at most once per thread
Summary: CoInitialize should only be called at most once per thread → CoInitialize should be called at most once per thread
Assignee: law → nobody
QA Contact: doronr → xpcom
And it should probably be changed to CoInitializeEx while we're at it. And ideally there should be a matching call to CoUnitialize. Currently CoInitialize is called a couple of times within the Mozilla code. This will also cause subsequent CoInitializeEx calls to fail if they specify a different threading model.

And that brings up another question. Should this use apartment model or multiple thread model?
jimm/bbondy, is this bug worth leaving open/fixing?
As long as the calls are balanced with CoUninitialize it's not a problem, but I'm not sure if it's expensive or not on the non first calls.  I'd be surprised if it was.  

And yes it has to be called on each thread so this seems like it could bring some regressions if we do find a better way. I'd probably vote to close.
Status: NEW → RESOLVED
Closed: 12 years ago
Resolution: --- → INCOMPLETE
You need to log in before you can comment on or make changes to this bug.