I tried both, but it didn't change anything. I guess the problem may come from the _text = newwchar_t[len+1]; since the debugger says it can't read the memory there, right after allocation is done...
Your advice will certainely be useful when my first bug will be solved ^^
CString has constructors for both char and wchar_t, so you can use either of them.
As for wcscpy_s, I never understands how it works and always crashes for me, even a simple example. Use wcscpy, this always works as expected (no, the problem is NOT in the line when you allocate memory).
I first wrote wcscpy, but it was marked as deprecated so... I changed it into wcscpy_s as advised by visual studio... hm... I will tried that once back home and be careful from who I accept advises :)
Could you post the relevant part of your class declaration for CString, wcscpy_s shouldn't ever get that upset!
BTW - codewalker did get you to correct CString toto((wchar_t*)"test"); to something more suitable.
But I just wanted to check that you picked up why it's so evil to lie to the compiler, that a multi-byte (or Ansi) string is actually Unicode. The terminator for a Unicode string is two 0 bytes, whereas a multi-byte string ends with just a single one (not enough for Unicode). On top of that, if it did display, it is likely to be some wierd characters from some random code page, rather than normal letters.
Using wcsncpy(_text, text, len + 1); solved the problem.
Using CString toto((wchar_t*)"test"); was effectively not a correct manner to write things. It doesn't crash but I get unexpected caracters in some situations.
Using CString toto( _T("test") ); solve this problem.
@Andywestken : I even tried to write a \0\0 at the end of the string, but it didnt help... :)
Anyway, as you pointed, it's not a correct way to write things.
I'm not sure what you call the relevant part of the class CString, so... I give it all. Anyway, so short...
The second parameter is the buffer length, not the string length. When I run your code, with just len, I get an assertion: "Buffer is too small"."
Basically, it's complaining that there's no room for it to add the null terminator it wants to add. (You don't get the assert because you're building the release target, going by your picture.)
Andy
PS Writing 2 \0s at the end of a Ansi string doesn't make it Unicode:
wchar_t* -> this string is using 2 bytes per char
char* -> this string is using 1 byte per char (except in special cases)
I saw the "Buffer is too small" in debug, but I couldn't get rid of it.
I tried to allocate a buffer with 1000 wchar_t, then copy "len" length. Still same problem.
So... I still can't make the wcscpy_s works, and since the wcsncpy works fine(that means : works as expected, where wcscpy_s doesn't !) I really don't want to know what's going on behind.
If you're interrested, I can send you the whole VS project.