ビジュアル スタジオ 2010
char 文字列にメモリを割り当て、その文字列 (ポインタ) を関数に渡し、メモリを解放しようとすると、「ヒープ破損が検出されました」という実行時エラーが発生します。
これは、メモリが返されたときにメモリを「解放済み」としてマークする関数の結果であると思われますが、これを回避する方法についてはまだ困惑しています。free() の呼び出しを単純に削除するのは気が進まない。
// this function takes a char string, converts it to a wide char string,
// then passes the converted string to another function.
void Class::WriteToLog(const char * msg)
{
// allocate memory
wchar_t * wMsg = (wchar_t*)malloc((strlen(msg) * sizeof(wchar_t)) + 1); // add one for null terminating char
// convert to wide char. This works fine, and returns 4 when msg is 4 chars long.
numChar = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, msg, strlen(msg), wMsg, strlen(msg));
wMsg[numChar] = L'\0'; // terminate string
// At this point wMsg contains the same thing msg contains. Works fine.
WriteToLogW(wMsg); // Works fine. Writes the string to a text file.
free(wMsg); // Heap Corruption Detected
}
void Class::WriteToLogW(const wchar_t * msg)
{
...
}