LPTSTR lpszStr = new TCHAR[1024] delete[] str;出错。
时间:2011-01-28 来源:mok'blog
这样写 delete会出错,不delete就会内存泄漏。
当不调用 GetWindowText时 delete是不会出错的
vs 提示 其原因可能是堆被损坏 好好的调用了一下GetWindowText怎么会堆被损坏呢?
BOOL GetWindowText(BSTR& bstrText) throw(){};明白了。
什么还会得到正确值呢?
通过 SysFreeString SysAllocString 它已经不是他了。

1 ::SysFreeString(bstrText);
2 bstrText = NULL;
3
4 int nLen = ::GetWindowTextLength(m_hWnd);
5
6
7 CTempBuffer<TCHAR> lpszText;
8 if(nLen>0)
9 {
10 ATLTRY(lpszText.Allocate(nLen+1));
11 if (lpszText == NULL)
12 {
13 return FALSE;
14 }
15
16 if(!::GetWindowText(m_hWnd, lpszText, nLen+1))
17 {
18 return FALSE;
19 }
20 }
21
22 bstrText = ::SysAllocString(T2OLE_EX_DEF(lpszText));
把 LPSTR 换成CSring 一切正常
相关阅读 更多 +