C++/CLI System.AccessViolationException when calling unmanaged function in managed class

前端 未结 1 1283
挽巷
挽巷 2021-01-21 15:07

I have a native callback function in C++, let\'s say something like this:

void ::CallbackFunction(void)
{
  // Do nothing
}

Now I have another

相关标签:
1条回答
  • 2021-01-21 15:32

    The native part was set up wrong:

    void ::SomeNativeFunction(void)
    {
      m_callback = std::tr1::bind(&::CallbackFunction, m_Tcy); // save in m_callback | m_Tcy is the class where CallbackFunction exists
      //this won't work
      m_Tcy->SomeManagedFunction(m_callback);
    }
    

    This worked for me:

    void ::SomeNativeFunction(void)
        {
          m_callback = std::tr1::bind(&::CallbackFunction, m_Tcy); // save in m_callback | m_Tcy is the class where CallbackFunction exists
          //this works, even tho the debugger dies on me when I try to debug this
          m_Tcy->SomeManagedFunction(&m_callback);
        }
    

    The callback stuff works, but still getting a error in the native main though:

    First-chance exception at 0x00007ffb2b59dd60 in *.exe: 0xC0000005: Access violation at location 0x00007ffb2b59dd60.
    Unhandled exception at 0x00007ffb2b59dd60 in *.exe: 0xC000041D: Exception during a user callback.
    

    Also, my visual studio 2010 crashes when debugging the callback (in my C++/CLI wrapper). If I wait long enough, it throws the following exceptions:

    Access violation reading location 0xfffffffffffffff8.
    
    0 讨论(0)
提交回复
热议问题