Swarts

I get "Unhandled exception at 0x00410f49 in aTest.exe: 0xC0000005: Access violation writing location 0x0000001c." when trying to execute the following block of code.

Code Snippet

void CParrallelPort::SetReadySignalHigh(bool signalVal)
{
m_ReadySignal = signalVal;
}


m_ReadySignal is also of type bool.

Any ideas on how to fix this Thanks


Re: Visual C++ General What causes this access violation?

Holger Grund

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





Re: Visual C++ General What causes this access violation?

Swarts

It took me forever to find, but you're right about the pointer. I just really need someone to tell me a way around it... how to fix this without redoing EVERYTHING.




Re: Visual C++ General What causes this access violation?

Bounty42

I'm having a similar error. Knowing only a little bit about programming, I don't really understand all of that. Can someone dumb it down for me and also tell me an easy way to fix it




Re: Visual C++ General What causes this access violation?

crescens2k

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.






Re: Visual C++ General What causes this access violation?

Bounty42

What do you mean by accessing the class memory How can you tell




Re: Visual C++ General What causes this access violation?

crescens2k

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.






Re: Visual C++ General What causes this access violation?

Bounty42

I think I understand. I've always had a tough time with pointers and classes.

So do I need to rewrite this program or is there a trick to getting around this with only a few more lines of code




Re: Visual C++ General What causes this access violation?

crescens2k

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.






Re: Visual C++ General What causes this access violation?

Bounty42

Awesome! That fixed it. Smile Thanks!

But now at a different part of the program it has an access violation reading from somewhere, rather than writing. Would the same thing be causing that problem




Re: Visual C++ General What causes this access violation?

crescens2k

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.






Re: Visual C++ General What causes this access violation?

ramki.v

Hi,
I am getting access violation in the bold line for the following code:

void strcatU(char* strd, const char* strs)
{
while(*strd) //OR you can find the length of this string and length can be used
{
strd++;
}

while(*strs)
{
*strd = *strs;
strd++;
strs++;
}

*strd = '\0';
}

Can you please tell me why this is the behaviour it is working fine on Turbo C, but not on VC++.