0xC0000005: Access violation reading location 0x00200074 ; happens when returning from function
I'm currently developing a program that reads strings from a text file in 8-bit ASCII mode, and I make a function to assign that string into a wchar_t*
Here I'm using atlconv.h and USES_CONVERSION macro to convert the string into wstring. So here is the code:
void CSampleProvider::getCopy(CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR *a, const string s) {
USES_CONVERSION;
wstring temp(A2W (s.c_str ()));
a->pszLabel = new WCHAR(temp.length()+1);
if (!a->pszLabel)
return;
wcscpy_s(a->pszLabel, temp.size()+1, (LPWSTR)temp.c_str());
::MessageBox(NULL,s.c_str(),"getCopy",0);
return;
}
I used a debugger to watch line by line. It works well (i.e. the content of a->pszLabel is as I expected, the same as the content of s) until it reaches return. As it returned, an error popped up:
First-chance exception at 0x770f3067 in CPTest.exe: 0xC0000005: Access violation reading location 0x00200074.
Unhandled exception at 0x770f3067 in CPTest.exe: 0xC0000005: Access violation reading location 0x00200074.
Does anyone kno开发者_高级运维w how to fix this? Please tell me. Your answers are highly appreciated :)
Thanks, Reinardus
EDIT: Oh yeah, the type CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR is a struct, and one of its member is pszLabel, which is a wchar_t*
a->pszLabel = new WCHAR(temp.length()+1);
returns a pointer to a new WCHAR
whose value is the length of your string plus one. You meant a->pszLabel = new WCHAR[temp.length()+1];
which returns a pointer to a new array of WCHAR
with the number of elements being the length of your string plus one.
These things are hard to debug without more complete information, but given that you see the error when returning from the function, my guess would be that a buffer overflow overwrites the return address, causing the code to jump to some arbitrary, non-rx memory location.
精彩评论