Re: memory instruction reference can't be read
mmlab_js schrieb:
I comment out each statement step by step to find which statement exists
error in release configuration. I find in the following function:
void VSocket::SendControlMessage(int nType,char *pAddress)
{
int n;
unsigned char data[500];
What is that magic number 500 here?
// Type of control packet
data[0] = (unsigned char)nType;
// Length of hostname
n = strlen(m_szLocalName);
data[1] = (unsigned char)n;
// Name of the sender host
memcpy(&data[2], m_szLocalName, n);
This code produces a buffer overflow if m_szLocalName is longer than 498
characters. And if it is longer than 255 characters, your logic fails anyway.
You should either correctly check the string length to prevent a failure, or use
something like CByteArray or std::vector<BYTE> to prevent memory overflow.
Did you run your app in the release mode in the debugger and checked m_szLocalName?
if(pAddress == NULL) {
SendTo(data, n+2, PORT_CONTROL, s_szRemoteAddress);
}
else {
SendTo(data, n+2, PORT_CONTROL, pAddress);
}
}
When the above function is called, the error "Unhandled exception in
StreamServer.exe:0xC0000005: Access Violation" will happen. If I comment out
the
// data[0] = (unsigned char)nType;
, the error is gone.
THis might even be a compiler bug. Have you installed the latest service pack of
Visual Studio and the STL fixes from Dinkumware (just in case you use stl, see
http://www.dinkumware.com/vc_fixes.html)?
I think that this assignment is legal. In debug configuration, there isn't
any error. Why??
The code is different in debug and release configurations.
As some other posters say, enable debug information in release mode (it is
somewhere in the linker settings) and run the release version in the debugger!
Norbert