Hello,
We develope a .NET plugin module for AutoCAD which calls some unmanaged code from itself, using COM. In this unmanaged code somewhere we create a thread and call CoInitialize inside of this thread (and, of course, CoUninitialize in pair). When AutoCAD is closed it crashes. If we comment out the CoInitialize calls, then AutoCAD ends successfully. Maybe this problem is not AutoCAD-specific one, but for us this is the first time it happened. So, what is possibly wrong there?
Hi,
Please, ensure you are not accessing the AutoCAD database from the new thread. It is allowed only from the AutoCAD thread itself.
Cheers,
Nothing interesting, something like this: "Unhandled exception at 0x21ed45c8 in acad.exe: 0xC0000005: Access violation reading location 0x21ed45c8."
No, all database operations are handled in the main thread. Generally we just load the plugin, create the thread and after AutoCAD closing - we get the crash.
How are you implementing the original call to the unmanaged code - using the Implements IExtensionApplication so you can ensure the cleanup is done in the terminate sub?
Just thinking that maybe its not been stopped in time for acad to properly shut down and because your operating in another thread when you call the coinit' that thread isn't killed in time to allow acad to shutdown.
Also there is this to note from MSDN;
Because there is no way to control the order in which in-process servers are loaded or unloaded, do not call CoInitialize, CoInitializeEx, or CoUninitialize from the DllMain function.
Im still fairly new to COM & .NET so these are just areas where I would look if were in your shoes. Not having the code makes it hard to debug.
Yes, we implement the IExtensionApplication interface and clean the things in Terminate. The thread ends successfully, as far as I understand.
It is not a big deal to comment out these CoInit calls, but I just want to understand what is the reason of the crash.
Having source code here makes no much sence, because it is quite complicated, I just try to describe the problem shortly 🙂