The +1
you added at the end of your malloc
will only allocate enough memory for the null terminator when working with char
s, as char
s are 1 byte (and hence the null terminator will take up one byte).
Since you're using wchar_t
, the null terminator will take up sizeof(wchar_t)
bytes, which will virtually always be greater than 1. Therefore, the malloc
call allocates insufficient memory. Re-structure your malloc
call to look like this:
wchar_t * wMsg = (wchar_t*) malloc((strlen(msg) + 1) * (sizeof(wchar_t));
This way the total number of bytes allocated leaves room for the null terminator.