如题,WaitForSingleObject可以等待被SetEvent()之后的Event对象,但是对于被ResetEvent()之后的Event对象,WaitForSingleObject会崩溃,直接崩溃,不返回。我是用matlab联合C编程(matalb调用mexw32文件),抛出的异常直接被matlab接住,matlab报告如下:
Assertion in void __cdecl `anonymous-namespace'::error(const struct `anonymous-namespace'::header *const ,const unsigned int,const void *const ,const unsigned int,const class boost::basic_format<char,struct std::char_traits<char>,class std::allocator<char> > &) at b:\matlab\foundation_libraries\src\fl\mem\alignment.cpp line 323:
The pointer passed to 'vector_check' is invalid
and does not appear to have come from any of the following routines:
vector_malloc, vector_calloc, vector_realloc
mxMalloc*, mxCalloc*, mxRealloc*This suggests one of the following has happened:
- the pointer has already been freed
- the pointer came from an incompatible allocator (e.g. new, malloc, utMalloc)
- the pointer didn't come from any allocator (e.g. the stack, uninitialized memory)
- a memory corruption destroyed the pointer or its headerTHIS DATA IS FOR INTERNAL DIAGNOSTIC PURPOSES ONLY
PID: 3444
NATIVE ALIGNMENT: 8 (0x8)
REQ ALIGNMENT: 32 (0x20)
ALIGNED POINTER: 003AF4A8
REQ SIZE: 0 (0)
HEADER ADDRESS: 003AF4A0
HEADER SIZE: 8 (0x8)
UNALIGNED POINTER: 003AF41C
HEADER->SIZE: 807794708 (0x3025f814)
HEADER->CHECK: 111 (0x6f)
HEADER->ALIGNMENT: 0 (0)
HEADER->OFFSET: 140 (0x8c)发生错误的代码如下:
WaitForSingleObject(infonode->hEvent, INFINITE);
如果hEvent所指的Event对象是激活的,这句代码能够正常执行,返回结果也是对的。
内核对象等待崩溃
Assertion in void __cdecl `anonymous-namespace'::error(const struct `anonymous-namespace'::header *const ,const unsigned int,const void *const ,const unsigned int,const class boost::basic_format<char,struct std::char_traits<char>,class std::allocator<char> > &) at b:\matlab\foundation_libraries\src\fl\mem\alignment.cpp line 323:
The pointer passed to 'vector_check' is invalid
and does not appear to have come from any of the following routines:
vector_malloc, vector_calloc, vector_realloc
mxMalloc*, mxCalloc*, mxRealloc*This suggests one of the following has happened:
- the pointer has already been freed
- the pointer came from an incompatible allocator (e.g. new, malloc, utMalloc)
- the pointer didn't come from any allocator (e.g. the stack, uninitialized memory)
- a memory corruption destroyed the pointer or its headerTHIS DATA IS FOR INTERNAL DIAGNOSTIC PURPOSES ONLY
PID: 3444
NATIVE ALIGNMENT: 8 (0x8)
REQ ALIGNMENT: 32 (0x20)
ALIGNED POINTER: 003AF4A8
REQ SIZE: 0 (0)
HEADER ADDRESS: 003AF4A0
HEADER SIZE: 8 (0x8)
UNALIGNED POINTER: 003AF41C
HEADER->SIZE: 807794708 (0x3025f814)
HEADER->CHECK: 111 (0x6f)
HEADER->ALIGNMENT: 0 (0)
HEADER->OFFSET: 140 (0x8c)发生错误的代码如下:
WaitForSingleObject(infonode->hEvent, INFINITE);
如果hEvent所指的Event对象是激活的,这句代码能够正常执行,返回结果也是对的。
内核对象等待崩溃
也许你在等的时候infonode可能被释放了,infonode->hEvent可能被销毁了。
SetEvent(infonode->hEvent);
WaitForSingleObject(infonode->hEvent, INFINITE); // 1
SetEvent(infonode->hEvent);
WaitForSingleObject(infonode->hEvent, INFINITE); // 2
ResetEvent(infonode->hEvent);
WaitForSingleObject(infonode->hEvent, INFINITE); // 3
前两个都能正常执行,第三个就崩溃了,唯一区别就是,我用了ResetEvent()
ResetEvent(infonode->hEvent);
WaitForSingleObject(infonode->hEvent, INFINITE);
这样会无限阻塞。
没有SetEvent(infonode->hEvent);
不需要ResetEvent(infonode->hEvent);
如果你需要立即ResetEvent(infonode->hEvent);设置成自动复位的