发现的内存泄露有两种,一种是上篇提到的,某些窗口没有用DestroyWindow来释放资源,而是自己写的一个ForceClose函数,这导致了某些函数没被调用,比如PostNcDestroy。
另外就是一个内嵌浏览器中使用到的BSTR字符串没有释放造成的问题。
String Manipulation Functions | Descriptions |
SysAllocString | Creates and initializes a string. |
SysAllocStringByteLen | Creates a zero-terminated string of a specified length. |
SysAllocStringLen | Creates a string of a specified length. |
SysFreeString | Frees a previously created string. |
SysReAllocString | Changes the size and value of a string. |
SysReAllocStringLen | Changes the size of an existing string. |
SysStringByteLen | Returns the length of a string in bytes. |
SysStringLen | Returns the length of a string. |
以上是BAIDU 百科BSTR的字符串操作函数
问题都出在分配了BSTR字符串之后,没有SysFreeString
谁的屁股谁自己擦,在哪个函数里分配,就在哪个函数里释放。
CString str_url = “XXXXX”;
_Browser->Navigate(str_url.AllocSysString(),
&flags,
&target_frame_name,
&post_data,
&headers);
这样的调用理所当然地造成泄漏,Navigate里又不会将BSTR字符串释放。
我的解决办法是用一个BSTR中间变量保存字符串,使用完之后用SysFreeString()来释放。
觉得还是不多废话了,泄露的原因就是BSTR字符串,BSTR字符串数组,下面是关于如何使用DISPPARAMS和SAFEARRAY的文章
http://blog.youkuaiyun.com/debehe/archive/2008/10/30/3187195.aspx