Void pointer functions access violation


#1

If I understood the documentation correctly this function adds a callback function on the message thread MessageManager::callFunctionOnMessageThread . Trying to test it out I always get an access violation. What am I doing wrong.
indent preformatted text by 4 spaces void* callback(void* ptr) { Logger::writeToLog("Callback\n"); return ptr; } void handleAsyncUpdate() override { void *userData = "test\n"; MessageManager::getInstance()->callFunctionOnMessageThread(static_cast<MessageCallbackFunction*>(callback(userData)), userData); } //The access violation is here when it reutrn func(parameter) if (isThisTheMessageThread()) return func (parameter);


#2

You’re not supposed to invoke the callback while calling callFunctionOnMessageThread. Notice how the function is passed directly, and not invoked here:

MessageManager::getInstance()->callFunctionOnMessageThread(callback, userData);

Also, I’m surprised it actually compiled without errors (or at least warnings). What compiler are you using?


#3

I’m using visual studio 2015 community and Xcode not at the same though. This was on vs 2015. Thanks. I recognize your username on kvr on my discussion there about hosts and Computer midi keyboards.


#4

Ah yes, that thread. Anyway, if your function were to return any other pointer type than void *, it would not have compiled, it seems.


#5

You are calling callback with userData and static casting its return value to a function pointer - which it isn’t - it’s a pointer to userData. Normally, you could not static cast a pointer to a function pointer - the only exception is void pointers. Hence, you are not getting a compiler error.

For the future:

  1. never use void pointers
  2. don’t mess around with casting pointers to function pointers. Usually a recipe for disaster

Have a look at the CallbackMessage class.