CHtmlView leaks memory if you do not release BSTRs in several methods
2007-12-25 13:53
453 查看
CHtmlView leaks memory if you do not release BSTRs in several methods
View products that this article applies to.function loadTOCNode(){}
Article ID | : | 241750 |
Last Review | : | November 21, 2006 |
Revision | : | 3.1 |
On This Page
![](http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif)
SYMPTOMS
![](http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif)
CAUSE
![](http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif)
RESOLUTION
![](http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif)
STATUS
![](http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif)
MORE INFORMATION
![](http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/downarrow.gif)
Steps to reproduce the behavior
var sectionFilter = "type != 'notice' && type != 'securedata' && type != 'querywords'";
var tocArrow = "/library/images/support/kbgraphics/public/en-us/downarrow.gif";
var depthLimit = 10;
var depth3Limit = 10;
var depth4Limit = 5;
var depth5Limit = 3;
var tocEntryMinimum = 1;
<style>.toc{display: none;}</style>
SYMPTOMS
loadTOCNode(1, 'symptoms');The following methods in CHtmlView cause a memory leak if called:
• | Navigate |
• | GetFullName |
• | GetType |
• | GetLocationName |
• | GetLocationURL |
• | LoadFromResource(LPCTSTR lpszResource) |
• | LoadFromResource(UINT nRes) |
![](http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/uparrow.gif)
Back to the top
CAUSE
loadTOCNode(1, 'cause');This problem is caused by bugs in the internal implementation in the first five functions listed in the "Summary" section. SysFreeString must be called in the first five functions on the allocated BSTRs. The last two functions cause a leak because CHtmlView::Navigate is called in both of these functions and CHtmlView::Navigate causes a leak.
![](http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/uparrow.gif)
Back to the top
RESOLUTION
loadTOCNode(1, 'resolution');To work around the leak, the functions listed in the "Summary" must be implemented in each class derived from CHtmlView. If you have several views derived some CHtmlView, consider making a new base class, such as CFixHtmlView, from CHtmlView, and then deriving the rest of the views from CFixHtmlView instead of CHtmlView:
1. | Inside the MFC application project, insert a new class. On the Insert menu, click New Class. In the New Class dialog box, make sure the Class Type is set to MFC Class. |
2. | Name the class CFixHtmlView, and then set the Base Class combo-box item to CHtmlView. |
3. | In the FixHtmlView.h file, add the following code to the class:class CFixHtmlView : public CHtmlView { //new code // Operations public: CString GetFullName() const; CString GetType() const; CString GetLocationName() const; CString GetLocationURL() const; void Navigate(LPCTSTR lpszURL, DWORD dwFlags = 0 , LPCTSTR lpszTargetFrameName = NULL , LPCTSTR lpszHeaders = NULL, LPVOID lpvPostData = NULL, DWORD dwPostDataLen = 0); //Add the following functions. This action prevents the leak because the //CHtmlView versions of LoadFromResource() call CHtmlView::Navigate(). //These need to be defined because CHtmlView::Navigate() is not virtual. BOOL LoadFromResource(LPCTSTR lpszResource); BOOL LoadFromResource(UINT nRes); //end new code |
4. | In the FixHtmlView.cpp file, add the following code://new code CString CFixHtmlView::GetFullName() const { ASSERT(m_pBrowserApp != NULL); BSTR bstr; m_pBrowserApp->get_FullName(&bstr); CString retVal(bstr); SysFreeString(bstr); // Added this line to prevent leak. return retVal; } CString CFixHtmlView::GetType() const { ASSERT(m_pBrowserApp != NULL); BSTR bstr; m_pBrowserApp->get_Type(&bstr); CString retVal(bstr); SysFreeString(bstr); // Added this line to prevent leak. return retVal; } CString CFixHtmlView::GetLocationName() const { ASSERT(m_pBrowserApp != NULL); BSTR bstr; m_pBrowserApp->get_LocationName(&bstr); CString retVal(bstr); SysFreeString(bstr); // Added this line to prevent leak. return retVal; } CString CFixHtmlView::GetLocationURL() const { ASSERT(m_pBrowserApp != NULL); BSTR bstr; m_pBrowserApp->get_LocationURL(&bstr); CString retVal(bstr); SysFreeString(bstr); // Added this line to prevent leak. return retVal; } void CFixHtmlView::Navigate(LPCTSTR lpszURL, DWORD dwFlags /* = 0 */, LPCTSTR lpszTargetFrameName /* = NULL */ , LPCTSTR lpszHeaders /* = NULL */, LPVOID lpvPostData /* = NULL */, DWORD dwPostDataLen /* = 0 */) { CString strURL(lpszURL); BSTR bstrURL = strURL.AllocSysString(); COleSafeArray vPostData; if (lpvPostData != NULL) { if (dwPostDataLen == 0) dwPostDataLen = lstrlen((LPCTSTR) lpvPostData); vPostData.CreateOneDim(VT_UI1, dwPostDataLen, lpvPostData); } m_pBrowserApp->Navigate(bstrURL, COleVariant((long) dwFlags, VT_I4), COleVariant(lpszTargetFrameName, VT_BSTR), vPostData, COleVariant(lpszHeaders, VT_BSTR)); SysFreeString(bstrURL); // Added this line to prevent leak. } BOOL CFixHtmlView::LoadFromResource(LPCTSTR lpszResource) { HINSTANCE hInstance = AfxGetResourceHandle(); ASSERT(hInstance != NULL); CString strResourceURL; BOOL bRetVal = TRUE; LPTSTR lpszModule = new TCHAR[_MAX_PATH]; if (GetModuleFileName(hInstance, lpszModule, _MAX_PATH)) { strResourceURL.Format(_T("res://%s/%s"), lpszModule, lpszResource); Navigate(strResourceURL, 0, 0, 0); } else bRetVal = FALSE; delete [] lpszModule; return bRetVal; } BOOL CFixHtmlView::LoadFromResource(UINT nRes) { HINSTANCE hInstance = AfxGetResourceHandle(); ASSERT(hInstance != NULL); CString strResourceURL; BOOL bRetVal = TRUE; LPTSTR lpszModule = new TCHAR[_MAX_PATH]; if (GetModuleFileName(hInstance, lpszModule, _MAX_PATH)) { strResourceURL.Format(_T("res://%s/%d"), lpszModule, nRes); Navigate(strResourceURL, 0, 0, 0); } else bRetVal = FALSE; delete [] lpszModule; return bRetVal; } //end new code |
5. | In each of the CHtmlView-derived classes' header and source files, change all the code that refers to CHtmlView to CFixHtmlView. To assist with this task, on the Edit menu, click Replace. |
6. | Be sure to add the following line of code in each of the CHtmlView-derived classes' header files before the class declaration:#include "FixHtmlView.h" //Add me before the class declaration. class CMyHtmlView : public CFixHtmlView { //class body }; |
7. | Rebuild the project. |
![](http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/uparrow.gif)
Back to the top
STATUS
loadTOCNode(1, 'status');Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.
![](http://support.microsoft.com/library/images/support/kbgraphics/public/en-us/uparrow.gif)
Back to the top
MORE INFORMATION
loadTOCNode(1, 'moreinformation');Steps to reproduce the behavior
loadTOCNode(2, 'moreinformation');1. | Create a new MFC SDI Application using AppWizard. Name it TestLeak1. |
2. | On Page 1 of AppWizard, change the Base class from CView to CHtmlView, and then click Finish. |
3. | Click the ResourceView tab and add a new menu item for the menu resource IDR_MAINFRAME. Add the item under the Help menu and change the caption to Test. |
4. | Use Class Wizard to add a COMMAND handler for the new menu item ID_HELP_TEST. Make sure that the class that handles the item is your CTestLeak1View class. |
5. | Add the following code to the handler:void CTestLeak1View::OnHelpTest() { for (int i = 0; i < 10000; i++) { CString str = GetFullName(); //add me } } |
6. | Build the project. |
相关文章推荐
- JQuery怎么知道一个元素是否隐藏或显示How do you test if something is hidden in jQuery?
- How to do IF NOT EXISTS in SQLite
- install fms in linux: error: if you have warned ERROR: Your distribution, unknown, is not supported by this
- JQuery怎么知道一个元素是否隐藏或显示How do you test if something is hidden in jQuery?
- 使用cocapods报错 [!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes
- How do I tell if a regular file does not exist in bash?
- JQuery怎么知道一个元素是否隐藏或显示How do you test if something is hidden in jQuery?
- Unable to start debugging on the web server. You do not have permission to debug the application. The URL for this project is in
- org.apache.hadoop.hbase.DoNotRetryIOException: Class org.apache.phoenix.coprocessor.MetaDataEndpointImpl cannot be loaded Set hbase.table.sanity.checks to false at conf or table descriptor if you want
- Do you know the sizes of these things in real memory?
- 51 You have not configured Oracle Managed Files (OMF) in your database. You do not want to scan the
- What will happen if we begin transaction in hibernate but do not commit it?
- 差别不在英语水平,而在汉语水平If you do not leave me, we will die together.
- to create table if table do not exist -- to backup and restore table in oracle
- JQuery怎么知道一个元素是否隐藏或显示How do you test if something is hidden in jQuery?
- How do you check if a variable is an array in JavaScript? [duplicate]
- JQuery怎么知道一个元素是否隐藏或显示How do you test if something is hidden in jQuery?
- can't create a new thread(errno 11) ;if you are not out of avaiable memory ,you can consult the manu
- osw ERROR. You do not have a legitimate version of java in your PATH
- Exception in thread "main" java.lang.RuntimeException: Hive metastore database is not initialized. Please use schematool (e.g. ./schematool -initSchema -dbType ...) to create the schema. If needed, do