简体   繁体   English

这个Windows API调用WaitForSingleObject有什么问题?

[英]What's wrong with this Windows API call WaitForSingleObject?

The process got crashed unstably in Windows 7. I use !analyze -v command in WinDbg for exception analysis. 该过程在Windows 7中不稳定崩溃。我在WinDbg使用!analyze -v命令进行异常分析。 It tells below information. 它告诉以下信息。 The exception is actually thrown by WaitForSingleObject function which is called by IrsSim!IrsNet_BlockOutput . 异常实际上是由IrsSim!IrsNet_BlockOutput调用的WaitForSingleObject函数抛出的。 WinDbg's exception analysis told me that it was INVALID_POINTER_READ error. WinDbg的异常分析告诉我,这是INVALID_POINTER_READ错误。

For the calling code, the pChannel->hMutex is not NULL . 对于调用代码, pChannel->hMutex不为NULL I already dumped it and checked its value. 我已经把它倾倒并检查了它的价值。

IRSNETRET IrsNet_BlockOutput( IRSNET    *pChannel)
{

// Check channel
    IRSNET_CHECK_CHANNEL(pChannel);

// Wait for synchronization mutex
    switch(WaitForSingleObject(pChannel->hMutex, INFINITE))
    {
...
}

<<<<<========== <<<<< ==========

FAULTING_IP: IrsSim!Channel::SendIrsMessage+285 [s:\\som5\\ics\\scsv\\isv\\test.u\\irssim\\irsiftransport.cpp @ 539] 00520ed5 8b06 mov eax,dword ptr [esi] FAULTING_IP:IrsSim!频道:: SendIrsMessage + 285 [s:\\ som5 \\ ics \\ scsv \\ isv \\ test.u \\ irssim \\ irsiftransport.cpp @ 539] 00520ed5 8b06 mov eax,dword ptr [esi]

EXCEPTION_RECORD: ffffffff -- (.exr 0xffffffffffffffff) ExceptionAddress: 77db4639 (ntdll!RtlDeactivateActivationContextUnsafeFast+0x00000058) EXCEPTION_RECORD:ffffffff - (.exr 0xffffffffffffffff)异常地址:77db4639(ntdll!RtlDeactivateActivationContextUnsafeFast + 0x00000058)
ExceptionCode: c0150010 ExceptionFlags: 00000001 NumberParameters: 3 Parameter[0]: 00000000 Parameter[1]: 07befc58 Parameter[2]: 00000000 ExceptionCode:c0150010 ExceptionFlags:00000001 NumberParameters:3参数[0]:00000000参数[1]:07befc58参数[2]:00000000

DEFAULT_BUCKET_ID: INVALID_POINTER_READ DEFAULT_BUCKET_ID:INVALID_POINTER_READ

PROCESS_NAME: IrsSim.exe PROCESS_NAME:IrsSim.exe

ERROR_CODE: (NTSTATUS) 0xc0150010 - The activation context being deactivated is not active for the current thread of execution. ERROR_CODE:(NTSTATUS)0xc0150010 - 当前执行的线程未激活的激活上下文无效。

EXCEPTION_CODE: (NTSTATUS) 0xc0150010 - The activation context being deactivated is not active for the current thread of execution. EXCEPTION_CODE:(NTSTATUS)0xc0150010 - 当前执行的线程未激活的激活上下文无效。

EXCEPTION_PARAMETER1: 00000000 EXCEPTION_PARAMETER1:00000000

EXCEPTION_PARAMETER2: 07befc58 EXCEPTION_PARAMETER2:07befc58

EXCEPTION_PARAMETER3: 00000000 EXCEPTION_PARAMETER3:00000000

STACK_TEXT: 07d2fce0 00520ed5 irssim!Channel::SendIrsMessage+0x285 07d2fd1c 00521072 irssim!CChannelArray::SendIrsMessage+0x132 07d2fd50 0052208a irssim!CNetLibInterface::SendIrsMessage+0xba 07d2fd78 005c01b6 irssim!CSendActivity::Execute+0x76 07d2fdac 005e0b3f irssim!SimulationThreadState::ExecuteOneActivity+0x11f 07d2fdf8 005cc937 irssim!CSimulationSubThreadState::ExecuteState+0x267 07d2fe8c 005ccf02 irssim!ThreadFctSubSimulation+0xf2 07d2fec4 73b1e3ee mfc90u!_AfxThreadEntry+0xf2 07d2ff4c 739f3433 msvcr90!_endthreadex+0x44 07d2ff84 739f34c7 msvcr90!_endthreadex+0xd8 07d2ff90 767d339a kernel32!BaseThreadInitThunk+0xe 07d2ff9c 77d69ed2 ntdll!__RtlUserThreadStart+0x70 07d2ffdc 77d69ea5 ntdll!_RtlUserThreadStart+0x1b STACK_TEXT:!!!!!07d2fce0 00520ed5 irssim频道:: SendIrsMessage + 0x285 07d2fd1c 00521072 irssim CChannelArray :: SendIrsMessage + 0x132 07d2fd50 0052208a irssim CNetLibInterface :: SendIrsMessage + 0xba 07d2fd78 005c01b6 irssim CSendActivity ::执行+ 0x76 07d2fdac 005e0b3f irssim SimulationThreadState :: ExecuteOneActivity + 0x11f 07d2fdf8 005cc937 irssim!CSimulationSubThreadState :: ExecuteState + 0x267 07d2fe8c 005ccf02 irssim!ThreadFctSubSimulation + 0xf2 07d2fec4 73b1e3ee mfc90u!_AfxThreadEntry + 0xf2 07d2ff4c 739f3433 msvcr90!_endthreadex + 0x44进行07d2ff84 739f34c7 msvcr90!_endthreadex + 0xd8 07d2ff90 767d339a KERNEL32!BaseThreadInitThunk + 0xe 07d2ff9c 77d69ed2 NTDLL !__ RtlUserThreadStart + 0x70 07d2ffdc 77d69ea5 ntdll!_RtlUserThreadStart + 0x1b

================================ ================================

After that I use !teb command to try get more stack information. 之后我使用!teb命令尝试获取更多堆栈信息。

0:011> k L=07beec2c 100 ChildEBP RetAddr 07bef54c 76be0bdd ntdll!NtWaitForMultipleObjects+0x15 07bef5e8 767d1a2c KERNELBASE!WaitForMultipleObjectsEx+0x100 07bef630 767d4208 kernel32!WaitForMultipleObjectsExImplementation+0xe0 07bef64c 767f80a4 kernel32!WaitForMultipleObjects+0x18 07bef6b8 767f7f63 kernel32!WerpReportFaultInternal+0x186 07bef6cc 767f7858 kernel32!WerpReportFault+0x70 07bef6dc 767f77d7 kernel32!BasepReportFault+0x20 07bef768 77da21d7 kernel32!UnhandledExceptionFilter+0x1af 07bef770 77da20b4 ntdll!__RtlUserThreadStart+0x62 07bef784 77da1f59 ntdll!_EH4_CallFilterFunc+0x12 07bef7ac 77d76ab9 ntdll!_except_handler4+0x8e 07bef7d0 77d76a8b ntdll!ExecuteHandler2+0x26 07bef7f4 77d76a2d ntdll!ExecuteHandler+0x24 07bef880 77d40143 ntdll!RtlDispatchException+0x127 07bef880 77db4639 ntdll!KiUserExceptionDispatcher+0xf 07befc34 76be0ad7 ntdll!RtlDeactivateActivationContextUnsafeFast+0x58 07befc38 76be0abc KERNELBASE!WaitForSingleObjectEx+0xde 07befc98 767d1194 KERNELBASE!WaitForSingle 0:011>ķL = 07beec2c 100 ChildEBP RetAddr 07bef54c 76be0bdd NTDLL NtWaitForMultipleObjects + 0×15 07bef5e8 767d1a2c KERNELBASE WaitForMultipleObjectsEx + 0x100的07bef630 767d4208 KERNEL32 WaitForMultipleObjectsExImplementation + 0xe0的07bef64c 767f80a4 KERNEL32 WaitForMultipleObjects的+为0x18 07bef6b8 767f7f63 KERNEL32 WerpReportFaultInternal + 0x186 07bef6cc 767f7858 KERNEL32!!! WerpReportFault + 0x70 07bef6dc 767f77d7 KERNEL32!BasepReportFault + 0×20 07bef768 77da21d7 KERNEL32!UnhandledExceptionFilter的+ 0x1af 07bef770 77da20b4 NTDLL!__ RtlUserThreadStart + 0X62 07bef784 77da1f59 NTDLL!_EH4_CallFilterFunc + 0×12 07bef7ac 77d76ab9 NTDLL!_except_handler4 +为0x8E 07bef7d0 77d76a8b NTDLL!ExecuteHandler2 + 0×26 07bef7f4 77d76a2d NTDLL!ExecuteHandler + 0x24 07bef880 77d40143 ntdll!RtlDispatchException + 0x127 07bef880 77db4639 ntdll!KiUserExceptionDispatcher + 0xf 07befc34 76be0ad7 ntdll!RtlDeactivateActivationContextUnsafeFast + 0x58 07befc38 76be0abc KERNELBASE!WaitForSingleObjectEx + 0xde 07befc98 767d1194 KERNELBASE!WaitForSingle ObjectEx+0xc3 07befcb0 767d1148 kernel32!WaitForSingleObjectExImplementation+0x75 ObjectEx + 0xc3 07befcb0 767d1148 kernel32!WaitForSingleObjectExImplementation + 0x75

07befcc4 005e3b6e kernel32!WaitForSingleObject+0x12 07befcc4 005e3b6e kernel32!WaitForSingleObject + 0x12

07befcd4 00520d3b IrsSim!IrsNet_BlockOutput+0x1e 07befcd4 00520d3b IrsSim!IrsNet_BlockOutput + 0x1e

07befd14 00521072 IrsSim!Channel::SendIrsMessage+0xeb 07befd48 0052208a IrsSim!CChannelArray::SendIrsMessage+0x132 07befd70 005c01b6 IrsSim!CNetLibInterface::SendIrsMessage+0xba 07befda4 005e0b3f IrsSim!CSendActivity::Execute+0x76 07befdf0 005cc937 IrsSim!SimulationThreadState::ExecuteOneActivity+0x11f 07befe84 005ccf02 IrsSim!CSimulationSubThreadState::ExecuteState+0x267 07befebc 73b1e3ee IrsSim!ThreadFctSubSimulation+0xf2 07beff44 739f3433 mfc90u!_AfxThreadEntry+0xf2 07beff7c 739f34c7 msvcr90!_endthreadex+0x44 07beff88 767d339a msvcr90!_endthreadex+0xd8 07beff94 77d69ed2 kernel32!BaseThreadInitThunk+0xe 07beffd4 77d69ea5 ntdll!__RtlUserThreadStart+0x70 07beffec 00000000 ntdll!_RtlUserThreadStart+0x1b 07befd14 00521072 IrsSim!频道:: SendIrsMessage +将0xEB 07befd48 0052208a IrsSim!CChannelArray :: SendIrsMessage + 0x132 07befd70 005c01b6 IrsSim!CNetLibInterface :: SendIrsMessage + 0xba 07befda4 005e0b3f IrsSim!CSendActivity ::执行+ 0x76 07befdf0 005cc937 IrsSim!SimulationThreadState :: ExecuteOneActivity + 0x11f 07befe84 005ccf02 IrsSim!CSimulationSubThreadState :: ExecuteState + 0x267 07befebc 73b1e3ee IrsSim!ThreadFctSubSimulation + 0xf2 07beff44 739f3433 mfc90u!_AfxThreadEntry + 0xf2 07beff7c 739f34c7 msvcr90!_endthreadex + 0x44进行07beff88 767d339a msvcr90!_endthreadex + 0xd8 07beff94 77d69ed2 KERNEL32!BaseThreadInitThunk + 0xe 07beffd4 77d69ea5 NTDLL!__ RtlUserThreadStart + 0x70 07beffec 00000000 ntdll!_RtlUserThreadStart + 0x1b

====================================>>>>>> ==================================== >>>>>>

This looks a lot like the 0xC015000f exception encountered in MFC applications ("The activation context being deactivated is not the most recently activated one.") 这看起来很像MFC应用程序中遇到的0xC015000f异常(“被激活的激活上下文不是最近激活的激活上下文。”)

In all cases where I have encountered this exception, the exception is not the primary issue. 在我遇到此异常的所有情况下,异常都不是主要问题。 It is a side effect of an earlier exception, usually an access violation, where the stack is not unwound properly. 它是早期异常的副作用,通常是访问冲突,其中堆栈未正确展开。 Somewhere a call frame that used a macro such as the AFX_MANAGE_STATE macro is missed in the exception handling. 某处使用宏(例如AFX_MANAGE_STATE宏)的调用帧在异常处理中被遗漏。 The result is that the next time the activation context is manipulated, say by another routine that results in a call to something like AFX_MAINTAIN_STATE2::~AFX_MAINTAIN_STATE2, the system detects a cookie mismatch and throws the exception. 结果是下次操作激活上下文时,例如另一个导致调用AFX_MAINTAIN_STATE2 :: ~AFX_MAINTAIN_STATE2之类的例程,系统会检测到cookie不匹配并抛出异常。

In your case you may either be causing an exception (most likely an AV) in one piece of code that then is manifested by the context exception. 在您的情况下,您可能会在一段代码中导致异常(很可能是AV),然后由上下文异常表明。 To trap the root cause, run the debugger with first chance exception handling enabled. 要捕获根本原因,请在启用第一次机会异常处理的情况下运行调试器。 That way the AV that is being trapped elsewhere up the call frame by someone perhaps using a try/catch(...) will be exposed. 这样,可能会使用try / catch(...)的某个人在调用帧的其他地方被捕获的AV将被暴露。 Since you appear to be threading, you may simply have a race condition on a memory access that causes the primary exception (if that is indeed what is happening). 由于您似乎是线程,因此您可能只是在内存访问上遇到一个竞争条件导致主要异常(如果确实发生了这种情况)。

I see in a previous post: "In fact, this problem comes from porting the program from 64-bit Win XP to 64-bit Win7. The compiler is switched therefore from VC6 to VC9. " 我在上一篇文章中看到:“事实上,这个问题来自于将程序从64位Win XP移植到64位Win7。因此编译器从VC6切换到VC9。”

This is not a bug in MFC. 这不是MFC中的错误。 MFC 6 did not include the activation context switching code (which is cookie based) that was added, I think, in Visual Studio 2005. So you would not encounter this exception. MFC 6不包括在Visual Studio 2005中添加的激活上下文切换代码(基于cookie)。因此,您不会遇到此异常。 We too thought the newer MFC had issues but in every case we have encountered, it was our code that caused the problem. 我们也认为较新的MFC存在问题,但在我们遇到的每种情况下,都是我们的代码导致了问题。 The original problems are masked by code flows that started with a try/catch (usually ...) that eventually called code that used one of the MFC manage state macros that then called more code where eventually the AV would occur. 原始问题被代码流掩盖,代码流以try / catch(通常是......)开始,最终调用代码使用其中一个MFC管理状态宏,然后调用更多代码,最终AV将发生。 Since the catch was way up the stack, and depending on the corruption, not all frames are unwound properly, the back side of the MFC macros are missed (some destructor failed to pop its context). 由于捕获是向上堆栈,并且取决于损坏,并非所有帧都被正确解开,MFC宏的背面被遗漏(一些析构函数无法弹出其上下文)。 To make matters worse (for debugging), the eventual context crash can occur anywhere in your code (we experienced a lot of them in CWnd's base window message processing routing method). 更糟糕的是(对于调试),最终的上下文崩溃可能发生在代码中的任何地方(我们在CWnd的基本窗口消息处理路由方法中经历了很多)。 We eventually created another tool for a user to run that would attach itself as a debugger to our (release target) executable that trapped first chance exceptions and created a dmp file so we could find the inital point where the exception occurred since a dump of the context exception almost never was useful since the original source of the problem was long since past execution. 我们最终为用户创建了另一个工具,它将自己作为调试器附加到我们的(释放目标)可执行文件中,该可执行文件捕获了第一次机会异常并创建了一个dmp文件,因此我们可以找到自从转储之后发生异常的初始点。上下文异常几乎从来没有用过,因为问题的原始来源早于执行过去。

The only way that call can fail in that manner is if 呼叫以这种方式失败的唯一方法是if

pChannel->hMutex

is invalid. 是无效的。 Either pChannel itself is invaild, or hMutex . pChannel本身是hMutex ,或hMutex Most likely the former. 最有可能是前者。

You should be checking if the handle is invalid not simply not NULL like: 您应该检查句柄是否无效,而不仅仅是NULL如:

if (myHandle != INVALID_HANDLE_VALUE)
{
    // do something
}

Usually the create handle function will return this value if there is an error. 通常,如果出现错误,create handle函数将返回此值。

looks like problem in context deactivation (thoughts based on windbg dump). 在上下文停用中看起来像问题(基于windbg转储的想法)。 Refer to http://blogs.msdn.com/b/junfeng/archive/2006/03/19/sxs-activation-context-activate-and-deactivate.aspx article. 请参阅http://blogs.msdn.com/b/junfeng/archive/2006/03/19/sxs-activation-context-activate-and-deactivate.aspx文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM