不完全确定我是否已经解决了这个问题,但这是我所看到的以及我认为正在发生的事情。
我有一个主要用 C 编写的 Win32 程序,它加载一个 C++ DLL。该 DLL 通过 COM 对象将数据从 C 程序传递到另一个应用程序——一个可能由 DLL 本身实例化的对象。所有这一切显然至少在 Windows XP 和 Windows 7 中运行良好(可能是 Win95 和 Win98,我需要更深入地回顾代码历史以找出引入此接口(interface)的时间),但在 Windows 10 中程序崩溃在 FreeLibrary() 调用此 DLL 期间。
在调试器中检查时,DLL_DETACH_PROCESS 似乎已成功处理(处理该消息时未执行任何代码)。崩溃发生在(或同时)从入口点离开代码。
如果我继续 Step In,我最终会看到一个名为 utilcls.h 的头文件,它似乎是 Borland C Builder 6 头文件之一。我相信其中的模板代码与被拆除的 COM 对象有关。 Unbind() 调用通过,这是崩溃前我可以单步执行的最后一行代码。
如果我使用调试器的 CPU 窗口并继续步进,剩下的一切似乎都与在崩溃发生前释放内存有关,但要达到那里需要大量的 CPU 步进。
崩溃引发 APPCRASH 异常 0xc0000602,返回 Combase.dll。
只要不为该 DLL 调用 FreeLibrary 即可使应用程序成功关闭,但我的假设是 FreeLibrary 调用很重要。
COM 对象在 FreeLibrary() 调用之前由数据共享应用程序释放,这允许该应用程序关闭。我目前的假设是,某些取消链接在较新的操作系统中发生的情况有所不同,这导致了崩溃,但我不知道如何确定。
我的问题:
如果其他人更清楚自己在做什么,那么导致崩溃的原因是什么?
尝试调试它的下一步是什么?我已经用尽了我对正在使用的调试环境的了解,并且对 COM 或 DLL 的了解不够深,不知道下一个要问的问题是什么。
RbMm 请求的一些调试器输出:
0:000:x86> t
ntdll_77b40000!RtlIsCriticalSectionLockedByThread+0x1b:
77b7256b c20400 ret 4
0:000:x86> t
combase!DecrementMTAUsageHelper+0x5b:
7527a2d6 85c0 test eax,eax
0:000:x86> r eax
eax=00000001
0:000:x86> t
combase!DecrementMTAUsageHelper+0x5d:
7527a2d8 0f859a000000 jne combase!DecrementMTAUsageHelper+0xfd (7527a378) [br=1]
0:000:x86> t
combase!DecrementMTAUsageHelper+0xfd:
7527a378 e89e9e0f00 call combase!CrashProcessWithWERReport (7537421b)
此时,堆栈大致如下所示:
ChildEBP RetAddr Args to Child
0019f9b8 7527a37c 063f4248 753d8448 00000000 combase!CrashProcessWithWERReport+0x35
0019f9e8 75292bfc 753d8448 7529257e 00000000 combase!DecrementMTAUsageHelper+0x101
(Inline) -------- -------- -------- -------- combase!DecrementMTAUsage+0x9
0019f9f0 7529257e 00000000 00000000 00000000 combase!CDllHost::MTAUninitializeApartmentOnly+0xe
0019fa08 7527543a 00000000 063f4248 00712410 combase!CDllHost::ClientCleanupFinish+0x4d
0019fa30 75276361 00000000 0019fa8c 00000000 combase!DllHostProcessUninitialize+0xa0
0019fa58 7527a452 000d06f6 00712410 00000000 combase!ApartmentUninitialize+0xe4
0019fa70 752c2a1e 000d06f6 00712e18 00712e80 combase!wCoUninitialize+0xd0
0019fa94 74ed3e58 00000003 74c17ff1 a6d0e607 combase!CoUninitialize+0x7e
0019fa9c 74c17ff1 a6d0e607 000b0792 74ed48f0 imm32!CtfImmCoUninitialize+0x48
0019fb7c 74809ea6 00050004 000d06f6 00000000 msctf!TF_Notify+0x581
0019fb98 748080dc 00050004 000d06f6 00000000 user32!CtfHookProcWorker+0x36
0019fbe0 74807fa6 0019fc34 0019fc24 00000000 user32!CallHookWithSEH+0x5c
0019fc08 77bb0006 0019fc24 00000018 0019fc80 user32!__fnHkINDWORD+0x26
0019fc38 710623fb 000b0792 04ff11aa 05480e70 ntdll!KiUserCallbackDispatcher+0x36
0019fc50 050364e4 000b0792 050376d8 05480e70 apphelp!DWM8AND16BitHook_DestroyWindow+0x2b
0019fc8c 05051007 00000000 05055034 00000001 myDLL!myCOMObject_tlbFinalize+0x408a4
0019fcb4 050511c6 0019fcd0 00000001 04ff1318 myDLL!myCOMObject_tlbFinalize+0x5b3c7
0019fcd8 04ff13d3 05055034 77badcce 04ff0000 myDLL!myCOMObject_tlbFinalize+0x5b586
0019fd00 77b807c6 04ff1318 04ff0000 00000000 myDLL+0x13d3
0019fd50 77b6aa5e 00000000 00000000 259704e5 ntdll!LdrpCallInitRoutine+0x43
0019fdb8 77b6e6c8 00000000 0071dd60 00000000 ntdll!LdrpProcessDetachNode+0xbb
0019fdd8 77b6e5af 25970745 0071e560 c000022d ntdll!LdrpUnloadNode+0x100
0019fe18 77b6e4f6 004afcc4 004ae3a4 04ff0000 ntdll!LdrpDecrementModuleLoadCountEx+0xa7
0019fe38 746e9d56 04ff0000 006e33c5 00000000 ntdll!LdrUnloadDll+0x86
0019fe4c 0049261c 04ff0000 00000000 00493034 KERNELBASE!FreeLibrary+0x16
0019fe64 00441895 004afc98 fffffffe 0019fee8 rpopdbg!_GetExceptDLLinfo+0x914bf
现在正在处理其余部分,但我想我需要弄清楚如何正确地对 COM 对象进行清理?也许是为了响应 DLL_DETACH_PROCESS?
最佳答案
The crash raises APPCRASH with exception 0xc0000602, referring back to Combase.dll
combase.dll 使用 0xc0000602 (STATUS_FAIL_FAST_EXCEPTION) 代码仅来自
void CrashProcessWithWERReport();
(使用此代码调用了 RaiseFailFastException)
CrashProcessWithWERReport 仅在 2 个条件下从 DecrementMTAUsageHelper 调用 - CoDecrementMTAUsage调用次数超过 CoIncrementMTAUsage或者(我几乎可以肯定是因为这个原因)DecrementMTAUsageHelper 在调用线程时调用保持 Loader 临界区 - 因此在 DLL 加载或卸载过程中。来自MSDN
Don't call CoDecrementMTAUsage during process shutdown or inside dllmain. You can call CoDecrementMTAUsage before the call to start the shutdown process.
所以我猜 - 一些代码调用 CoDecrementMTAUsage在你的 DLL 卸载过程中(当你调用 FreeLibrary 时)
你的DLL不能直接调用CoIncrementMTAUsage/CoDecrementMTAUsage因为这个新的 API 从 win 8 开始就存在(还要检查你在 win 8.1 上的代码——我想也会崩溃),但是这个 api 可以从其他系统组件间接调用。
我可以假设您的 DLL 不直接释放一些已用资源,或者您在 DLL 仍持有一些资源时调用 FreeLibrary(因此您调用 FreeLibrary 时没有对 DLL 进行适当的清理调用) 并且因此该资源在卸载过程中开始释放 (CoDecrementMTAUsage)
what are the next steps in trying to debug this?
您需要使用符号文件进行调试(比如使用 winDbg)。在 DecrementMTAUsageHelper、CoDecrementMTAUsage 处设置断点并且可能是 CoIncrementMTAUsage - 我调用RtlIsCriticalSectionLockedByThread 是否正确返回 TRUE(此 api 从 DecrementMTAUsageHelper 开始调用)。
在任何情况下,在 DecrementMTAUsageHelper 调用点(就在崩溃之前)发布线程调用堆栈,也可能在 CoIncrementMTAUsage 上发布
-------------------- 编辑 -------------------- ----
通过查看堆栈跟踪可见,您的 DLL 从 DllMain 调用 DestroyWindow。
apphelp!DWM8AND16BitHook_DestroyWindow
这是错误的两个原因 - 首先 - 阅读 this article -
The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification. You can't do anything with thread affinity in your DLL_PROCESS_ATTACH or DLL_PROCESS_DETACH handler since you have no guarantee about which thread will be called upon to handle these process notifications. The classic example of this, which I'm told the Developer Support team run into with alarming frequency, is a DLL that creates a window in its DLL_PROCESS_ATTACH handler and destroys it in its DLL_PROCESS_DETACH handler.
但您的崩溃是由于其他原因造成的,未在文章中列出 - DllMain 有很多 restrictions ,里面不能叫什么。尽管 DestroyWindow 没有直接在此处列出,但正如您的情况所示 - 这是非法调用(即使我们在创建此窗口的同一线程上调用) - 当您的窗口被销毁时 imm32 .CtfImmNotify(msctf!TF_Notify) 被调用
0019fa9c 74c17ff1 a6d0e607 000b0792 74ed48f0 imm32!CtfImmCoUninitialize+0x48
0019fb7c 74809ea6 00050004 000d06f6 00000000 msctf!TF_Notify+0x581
0019fb98 748080dc 00050004 000d06f6 00000000 user32!CtfHookProcWorker+0x36
0019fbe0 74807fa6 0019fc34 0019fc24 00000000 user32!CallHookWithSEH+0x5c
结果CoUninitialize 从 DllMain 调用 !
来自 MSDN
do not call CoInitialize, CoInitializeEx, or CoUninitialize from the DllMain function.
这里是FINAL CoUninitialize称为 DecrementMTAUsage,它通过调用 RtlIsCriticalSectionLockedByThread 和调用的 CrashProcessWithWERReport 确定我们在加载程序锁内。
解决方案?
当然最好的办法是修复 DLL,但如果这不可能 - 考虑下一个“hack”就可以了
HRESULT hr = CoInitialize(0); // asume that we in STA
FreeLibrary(hDLL);
if (0 <= hr) CoUninitialize();
有了这个CoUninitialize当然无论如何都会从 imm32!CtfImmCoUninitialize 调用,但这将是 NOT FINAL 未初始化,因此 DecrementMTAUsage 将不会被调用
关于c - 在 Win 10 而非 Win 7 中卸载 DLL 时调试崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41008037/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我最近决定从我的系统中卸载RVM。在thispage提出的一些论点说服我:实际上,我的决定是,我根本不想担心Ruby的多个版本。我只想使用1.9.2-p290版本而不用担心其他任何事情。但是,当我在我的Mac上运行ruby--version时,它告诉我我的版本是1.8.7。我四处寻找如何简单地从我的Mac上卸载这个Ruby,但奇怪的是我没有找到任何东西。似乎唯一想卸载Ruby的人运行linux,而使用Mac的每个人都推荐RVM。如何从我的Mac上卸载Ruby1.8.7?我想升级到1.9.2-p290版本,并且我希望我的系统上只有一个版本。 最佳答案
我刚刚安装了带有RVM的Ruby2.2.0,并尝试使用它得到了这个:$rvmuse2.2.0--defaultUsing/Users/brandon/.rvm/gems/ruby-2.2.0dyld:Librarynotloaded:/usr/local/lib/libgmp.10.dylibReferencedfrom:/Users/brandon/.rvm/rubies/ruby-2.2.0/bin/rubyReason:Incompatiblelibraryversion:rubyrequiresversion13.0.0orlater,butlibgmp.10.dylibpro
我正在运行Ubuntu11.10并像这样安装Ruby1.9:$sudoapt-getinstallruby1.9rubygems一切都运行良好,但ri似乎有空文档。ri告诉我文档是空的,我必须安装它们。我执行此操作是因为我读到它会有所帮助:$rdoc--all--ri现在,当我尝试打开任何文档时:$riArrayNothingknownaboutArray我搜索的其他所有内容都是一样的。 最佳答案 这个呢?apt-getinstallri1.8编辑或者试试这个:(非rvm)geminstallrdocrdoc-datardoc-da
我已经通过提供MagickWand.h的路径尝试了一切,我安装了命令工具。谁能帮帮我?$geminstallrmagick-v2.13.1Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingrmagick:ERROR:Failedtobuildgemnativeextension./Users/ghazanfarali/.rvm/rubies/ruby-1.8.7-p357/bin/rubyextconf.rbcheckingforRubyversion>=1.8.5...yescheckingfor/
我正在使用macos,我想使用ruby驱动程序连接到sqlserver。我想使用tiny_tds,但它给出了缺少free_tds的错误,但它已经安装了。怎么能过这个?~brewinstallfreetdsWarning:freetds-0.91.112alreadyinstalled~sudogeminstalltiny_tdsBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtiny_tds:ERROR:Failedtobuildgemnativeextension.完整日志如下:/System
我正在使用PostgreSQL9.1.3(x86_64-pc-linux-gnu上的PostgreSQL9.1.3,由gcc-4.6.real(Ubuntu/Linaro4.6.1-9ubuntu3)4.6.1,64位编译)和在ubuntu11.10上运行3.2.2或3.2.1。现在,我可以使用以下命令连接PostgreSQLsupostgres输入密码我可以看到postgres=#我将以下详细信息放在我的config/database.yml中并执行“railsdb”,它工作正常。开发:adapter:postgresqlencoding:utf8reconnect:falsedat
如何解决这个错误:$rvminstall1.9.3Searchingforbinaryrubies,thismighttakesometime.Nobinaryrubiesavailablefor:osx/10.9/x86_64/ruby-1.9.3-p547.Continuingwithcompilation.Pleaseread'rvmhelpmount'togetmoreinformationonbinaryrubies.Checkingrequirementsforosx.Certificatesin'/usr/local/etc/openssl/cert.pem'arealr