I've got an wide char string , where the content of it is read from a rich edit control. Because the corrupted content of strKeyW, application lately crashes ; and unfortunately I can't check why exactly invalid data is written to it (the current debugging comes from a DUMP file and I can't reproduce the crash by myself on my local computer neither on the test environment). Anyway, is it there any method(s) that could help me to validate the strKeyW so I can avoid future use of unreadable memory ?
LPWSTR strKeyW = new WCHAR[nLength];
m_HiddenRTFCtrl.SendMessage(EM_GETSELTEXT, (WPARAM)0, (LPARAM)strKeyW);

nLengthcome from? Is it large enough to hold the text from the control, including the terminating NUL character? You can check that against the return value from SendMessage.nLength, no , it's not large enough to hold the data (at least I suppose so) , but it's neither 0 or negative value. But what's more strange I know the source where from the data is included in m_HiddenRTFCtrl , and after any test on local machine this sendmessage didn't failed. Also it's possible that the Dump file is .. I don't know , maybe not accurate . For any cases I want to add a validation function , at least till I don't get any more dumps or data from client. So far my question is still if I can avoid any other situations like this one by checking the incoming string.