void StaticControl::addText(LPWSTR text)
{
lpCurrentText = (LPWSTR)malloc((GetWindowTextLength(hStatic) + 1) * sizeof(LPWSTR));
GetWindowText(hStatic, lpCurrentText, GetWindowTextLength(hStatic) + 1);
lpInput = text;
chInput = (char*)malloc(sizeof (lpInput) *sizeof(LPWSTR)/sizeof(char*));
chCurrent = (char*)malloc(sizeof (lpCurrentText) *sizeof(LPWSTR)/sizeof(char*));
wcstombs(chCurrent, lpCurrentText, wcslen(lpCurrentText) + 1);
wcstombs(chInput, lpInput, wcslen(lpInput) + 1);
strcat(chCurrent, chInput);
lpNewText = (LPWSTR)malloc(strlen(chCurrent) * sizeof(char*)); //this is where it crashes
mbstowcs(lpNewText, chCurrent, strlen(chCurrent) + 1);
SetWindowText(hStatic, lpNewText);
return;
}
//where
HWND hStatic;
LPWSTR lpCurrentText;
LPWSTR lpInput;
LPWSTR lpNewText;
char* chInput;
char* chCurrent;
这段代码可以很好地向控件添加文本,直到字符串变成大约 20 个字符长,程序崩溃。单步执行程序,它在我为 lpNewText 缓冲区分配内存的地方崩溃。我不知道怎么了。Visual Studio 崩溃时将我带到 malloc.h 标头。