void CParrallelPort::SetReadySignalHigh(bool signalVal)
{
m_ReadySignal = signalVal;
}
m_ReadySignal is also of type bool.
Any ideas on how to fix this Thanks
void CParrallelPort::SetReadySignalHigh(bool signalVal)
{
m_ReadySignal = signalVal;
}
this is probably 0 or another invalid pointer.
Consider you have:
CParrallelPort* p = SomePointer();
p->SetReadySignalHigh();
and SetReadySignalHigh is non-virtual, the code effectively translates to something like
CParallelPort_SetReadySignalHigh(p); // non-virtual call, p not dereferenced here
Most compiler toolchains will simply let you call a non-virtual member function via an invalid pointer - it is still undefined behavior and should be avoided if possible.
The assignment results in a single write to memory at p + offsetof(m_ReadySignal,CParrallelPort). This is very likely the cause of the crash.
You should take a look at the call stack in the debugger and check the address of the value used to invoke the member function.
-hg
Basically, check the pointer for your class. it is invalid.
Using the following class
class CFred
{
private:
int a;
public:
void MessageMe(wchar_t* pMessage) {MessageBox(NULL, pMessage, L"Message", MB_OK);};
void SetA(int nA) {a = nA;};
};
Surprisingly enough, the following will work.
CFred* pFred = NULL;
pFred->MessageMe(L"Hello");
This is because you are not accessing any of the class memory.
On the other hand the following will not work.
CFred* pFred = NULL;
pFred->SetA(2);
This is because the method is trying to access memory which is protected by the operating system.
If you try to call a method that accesses memory in the class.
The second example I gave, it tried to set a to the value 2, since the pointer was null then this would try to access the memory around that area and cause an access violation.
Make sure you you create an instance of the class before you access it.
class CFred
{
int a;
public:
void SetA(nA) {a = nA;};
};
int main()
{
CFred* pFred;
pFred = new CFred();
pFred->SetA(2);
delete pFred;
}
The above sample will work because you are allocating and setting the pointer to a memory address you are allowed to access.
It isn't so easy to say. Access violations are caused by trying to read from or write to memory which you can't access. It is not necessarily classes which can cause this.
In general, make sure that if you are using a pointer then you must assign something to it by using new or giving it an address of a currently existing variable. I would suggest, if you can, to post the code which is causing this access violation.