InvalidCastException when calling SevenZipExtractor.Dispose()

Oct 25, 2010 at 9:20 PM

I'm using the SevenZipExtractor as illustrated in the sample project, but when calling Dispose() on the extractor object I get the following InvalidCastException exception:

Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'SevenZip.IInArchive'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{23170F69-40C1-278A-0000-000600600000}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Furthermore, when shutting down, I get the following warning from Visual Studio (most likely due to the COM object not having been cleaned up properly):

Managed Debugging Assistant 'RaceOnRCWCleanup' has detected a problem in <app>:

Additional Information: An attempt has been made to free an RCW that is in use.  The RCW is in use on the active thread or another thread.  Attempting to free an in-use RCW can cause corruption or data loss.

Oct 27, 2010 at 6:05 AM

Send me your complete solution code so that I can diagnose the problem.

Nov 4, 2010 at 12:46 AM

Sorry for the slow reply. I need to create a repo of this in a standalone app since I'm not able to send you the entire solution I'm working on. This hasn't happened to you in your sample code at all? I'm using VS2010, in case that makes any difference.l

Apr 3, 2011 at 10:28 PM

@dreijer I faced a similiar issue: it appears that SevenZipExtractor works only in a single thread environment: this means that you can not create an instance of a SevenZipExtractor in one thread and use this instance in another. Doing this I got the "Unable to cast COM object of type 'System.__ComObject' to interface type 'SevenZip.IInArchive'" error.

I fixed the issue instancating and using SevenZipExtractor in the same thread.

Please tel me if that helps.

Apr 3, 2011 at 11:08 PM

Ah, that makes sense. It's probably using a single-threaded COM apartment, which won't work across multiple threads.

I'll take a look at the code and see if I'm using it from multiple threads.


Apr 7, 2011 at 9:02 PM

I looked through the code and it ought to be already only accessing the SevenZipExtractor object on the main GUI thread, so I'm still a little puzzled as to what's up :S

Dec 14, 2013 at 1:14 PM
I had this when I tried to call extractor.Dispose() in a class destructor that the garbage collector would call whenever it felt like doing its job. At program shutdown, the destructor would be called too late (after unloading the dll?) and this exception would be thrown. I resolved it by ensuring every instance of the extractor be disposed of before allowing the shut down to proceed.