SUGGESTED FIX
MSDN on CoInitialize states that:
a) "You need to initialize the COM library on a thread before you call any of the library functions"
b) "each successful call to CoInitialize [...] must be balanced by a corresponding call to CoUninitialize"
So there could be several ways to fix it.
1) To initialize COM library on every call to the Win32ShellFolder2's native methods doGetColumnInfo and doGetColumnValue, and uninitialize it on its termination. This was first what I tried and it worked correctly but slowed down performance dramatically: directories with at least 50-60 files were almost impossible to browse in details view mode. It made me to refuse that, but developing it helped me to put all required native code in order.
2) To initialize COM library on the thread where the native Win32ShellFolder's methods are called and uninitialize it on the thread termination. The problem was that this thread was the event dispatch one, and there was no control on when it started and no notifications when it stopped: it's known that AWT can start and stop EDT many times during the application running, so this approach required embbedding my fix into the AWT's EDT management code (since CoUninitialize should be called on the same thread where CoInitialized was called), what was inacceptable on my mind.
3) To create a special thread for calling COM functions that would provide correct initialization/uninitialization of the COM library. So in Win32ShellFolder2, I created two native call wrappers (inner classes ComGetColumnInfo and ComGetColumnValue), and an inner class ComCallWrapper that provides theirs execution on its own thread and transparent COM initialization/uninitialization for that thread. In my opinion, its solves the problem optimally: it allows consecutive calls to perform without reinitializing COM, and doesn't eat resources between series of calls, what fits the typical behavior of JFileChooser: massive file details reading on open or directory change, and then long period of idling while the user observes the shown info.
|