Reputation: 147
I'm experiencing an access violation error when I try to call non static function.
my .h
file looks like this.
class World
{
public:
World();
virtual ~World();
static void CALLBACK DispatchCallback(
SIMCONNECT_RECV *pData,
DWORD cbData,
void *pContext
);
void Process(SIMCONNECT_RECV *pData, DWORD cbData);
virtual void frameEvent();
virtual void init();
};
Now in my .cpp file the init() function I call a function that return data to my callback function.
SimConnect_CallDispatch(hSimConnect, DispatchCallback, NULL);
Which sends data to the DisPatchCallback
function.
In this function the following code resides:
void CALLBACK World::DispatchCallback(
SIMCONNECT_RECV *pData,
DWORD cbData,
void *pContext)
{
World *pThis = reinterpret_cast<World*>(pContext);
pThis->Process(pData, cbData);
}
this function is a static function which creates a World object to call the Process function.
This works but it break on the line where it wants to access the frameEvent function.
void World::Process(SIMCONNECT_RECV *pData, DWORD cbData)
{
HRESULT hr;
switch(pData->dwID)
{
case SIMCONNECT_RECV_ID_EVENT_FRAME:
frameEvent(); //it breaks here frameEvent is a non static function
break;
}
}
Access violation reading location 0x00000000.
Can someone point me to the right direction as to solve this issue.?
If you wonder I'm writing a plugin for Microsoft Flight Simulator X.
I'm trying to implement simconnect.h
in a oo way. Msdn shows an example that I'm trying to implement.
class CName
{
void Dispatch();
static void DispatchCallback(
SIMCONNECT_RECV *pData,
DWORD cbData,
void *pContext
);
void Process(SIMCONNECT_RECV *pData, DWORD cbData);
HANDLE hSimConnect; // use SimConnect_Open to set this value.
};
void CName::Dispatch()
{
::SimConnect_Dispatch(hSimConnect, &CName;::DispatchCallback, this);
}
// static function
void CName::DispatchCallback(
SIMCONNECT_RECV *pData,
DWORD cbData,
void *pContext)
{
CName *pThis = reinterpret_cast<CName*>(pContext);
pThis->Process(pData, cbData);
}
void CName::Process(SIMCONNECT_RECV *pData, DWORD cbData)
{
// do processing of SimConnect data
}
I hope I gave enough information.
Upvotes: 2
Views: 1009
Reputation: 20272
It breaks because this
is NULL.
Check this portion of your code:
World *pThis = reinterpret_cast<World*>(pContext);
pThis->Process(pData, cbData);
If the reinterpret_cast
fails gets a NULL - it returns NULL, and you don't check for it.
Upvotes: 2
Reputation: 993105
Your use of:
SimConnect_CallDispatch(hSimConnect, DispatchCallback, NULL);
seems very suspicious. What's the NULL
parameter mean in the call to that function? Is that pContext
?
Upvotes: 2
Reputation: 400274
You're passing NULL
as the context parameter to SimConnect_CallDispatch
, so your callback has no idea which World
object to call Process
on -- how could it possibly if you don't tell it? Change the call to pass in this
as the context parameter, like the example does:
SimConnect_CallDispatch(hSimConnect, DispatchCallback, this);
Upvotes: 8