如题,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对象是激活的,这句代码能够正常执行,返回结果也是对的。
内核对象等待崩溃

解决方案 »

  1.   

      - the pointer has already been freed
    也许你在等的时候infonode可能被释放了,infonode->hEvent可能被销毁了。
      

  2.   

    没有,我在调试时一直盯着所有变量,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() 
      

  3.   

    确定只有这里使用了infonode->hEvent?会不会有其它地方在你复位infonode->hEvent后有什么操作。仅仅单独这几个代码是没有问题的。要知道
    ResetEvent(infonode->hEvent);
    WaitForSingleObject(infonode->hEvent, INFINITE);
    这样会无限阻塞。
      

  4.   

    我是在其他地方激活Event对象,使得这个线程(辅助线程)可以继续执行。我突然想起一个问题,我创建这个Event对象的线程(主线程,短暂存在)会退出,但是这些信息是全局保存的,不会丢失,但是我想是不是创建这个Event对象的线程退出,这个Event对象也会被销毁?我没用过Event,不知道这个东西与创建者有没有关系,还请指教
      

  5.   

    CreateEvent第二个参数是TRUE表示手动复位,为FALSE表示自动复位,哪里有等待这个Event的线程的话,线程退出Event自动复位。
      

  6.   

    噢,我本来是设置的自动复位,后来为了测试才改成了手动复位。我现在程序改了改,这个Event对象是在辅助线程创建,然后在辅助线程等待这个Event对象,由其他线程(主线程)激活这个对象,还是不行,辅助线程中等待这个Event对象时,如果这个Event对象是未激活的,还是崩溃。但是主线程我也创建了一个,在主线程等待就没问题。
      

  7.   

    试过,不行,还是会崩溃。我在另外一个帖子里说了另外一种情况,这两种情况的代码结构不一样。这个帖子里说的这个情况,不论怎么样,只要是等待未触发的Event内核对象,WaitForSingleObject一定会崩溃,除非我在之前先SetEvent一下。
      

  8.   

    ResetEvent(infonode->hEvent);
    没有SetEvent(infonode->hEvent);
    不需要ResetEvent(infonode->hEvent);
    如果你需要立即ResetEvent(infonode->hEvent);设置成自动复位的