IHTMLDocument2.write()memoryleak [英] IHTMLDocument2.write() memoryleak

查看:90
本文介绍了IHTMLDocument2.write()memoryleak的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在MFC CDHtmlDialog中我正在使用IHTMLDocument2的写入功能更新HTML内容。


我的CDHTMLDialog是一个无模型对话框。我在整个应用程序中使用相同的对话框对象。





每次我向嵌入式浏览器写东西,在任务管理器中查看内存

的使用情况,使用的内存似乎每个
IHTMLDocument2 :: write / close大约750 KB。如果我将通话注释为

m_spHtmlDoc->写(pSA); 内存使用率更正常。


我使用下面的代码来更新HTML内容。

 IMPLEMENT_DYNCREATE(CEzHtmlChildDlg,CDHtmlDialog)

CEzHtmlChildDlg :: CEzHtmlChildDlg(CWnd * pParent / * = NULL * /)
:CDHtmlDialog(CEzHtmlChildDlg :: IDD,0,pParent)
{
CDHtmlDialog :: Create(CEzHtmlChildDlg :: IDD,pParent);
}

CEzHtmlChildDlg :: ~CEzHtmlChildDlg()
{
}

void CEzHtmlChildDlg :: DoDataExchange(CDataExchange * pDX)
{
CDHtmlDialog :: DoDataExchange(pDX);
}

BOOL CEzHtmlChildDlg :: OnInitDialog()
{
SetHostFlags(m_dwHostFlags
| DOCHOSTUIFLAG_DIALOG // MSHTML无法选择文本中的文本form
| DOCHOSTUIFLAG_DISABLE_HELP_MENU // MSHTML不会将帮助菜单项添加到容器的菜单。
| DOCHOSTUIFLAG_SCROLL_NO // MSHTML没有滚动条。
| DOCHOSTUIFLAG_DISABLE_SCRIPT_INACTIVE // MSHTML不执行任何操作脚本直到完全激活。
| DOCHOSTUIFLAG_THEME //指定托管浏览器应该为它显示的页面使用主题
// | DOCHOSTUIFLAG_NOTHEME
);

CDHtmlDialog :: OnInitDialog();

//禁用放弃目标
//不允许用户将快捷方式放到此窗口并让浏览器
//导航以显示该网站
m_pBrowserApp-> put_RegisterAsDropTarget(VARIANT_FALSE);



EnableAutomation();
LPDISPATCH pDisp = GetIDispatch(FALSE);
SetExternalDispatch(pDisp);

返回TRUE; //返回TRUE,除非你将焦点设置为一个控件
}

BOOL CEzHtmlChildDlg :: PreTranslateMessage(MSG * pMsg)
{
// TODO:添加你的这里有专门的代码和/或调用基类
if(pMsg-> message == WM_KEYDOWN&&
(pMsg-> wParam == VK_ESCAPE || pMsg-> wParam == VK_RETURN || pMsg-> wParam == VK_F5))
返回true;

if(pMsg-> message == WM_RBUTTONDOWN || pMsg-> message == WM_RBUTTONDBLCLK)
返回TRUE;

返回CDHtmlDialog :: PreTranslateMessage(pMsg);
}

BEGIN_MESSAGE_MAP(CEzHtmlChildDlg,CDHtmlDialog)
END_MESSAGE_MAP()

BEGIN_DHTML_EVENT_MAP(CEzHtmlChildDlg)

END_DHTML_EVENT_MAP()

// CEzHtmlChildDlg消息处理程序

void CEzHtmlChildDlg :: SetHTML(CString strHtml)
{
//创建一个safearray来存储HTML文字
SAFEARRAY * pSA;
SAFEARRAYBOUND saBound = {1,0};
pSA = SafeArrayCreate(VT_VARIANT,1,& saBound);

CComVariant varDummy;

//将HTML复制到唯一元素
VARIANT * pVar;
CComBSTR bstrHTML = strHtml; //加载文本
varDummy = bstrHTML; // ..变种

SafeArrayAccessData(pSA,(void **)& pVar); //访问safearray数据
pVar [0] = varDummy; //设置文本数据
SafeArrayUnaccessData(pSA); //释放访问权限

//将HTML写为文档的新文本
m_spHtmlDoc-> write(pSA); //覆盖HTML
m_spHtmlDoc-> close(); //更新浏览器

SafeArrayDestroy(pSA); //使用safearray完成
}

m_spHtmlDoc 是CDhtmlDialog的成员变量。


我在写文件时遇到的错误是什么?










解决方案

IHTMLDocument2 .write方法说"请勿使用
写入方法或
writeln
方法,除非您先拨打
< a href ="https://msdn.microsoft.com/en-us/library/ms536652%28v=vs.85%29.aspx"> 打开 方法,清除当前文档窗口并删除所有变量。"


只是猜测,但也许这是相关的。


Hi,

In MFC CDHtmlDialog I am updating HTML content using IHTMLDocument2's write function.

My CDHTMLDialog is a modelless dialog. I am using same dialog object throughout the application.


Everytime I write something to the embedded browser, and look at memory
usage in task manager, the used memory seems to go up by about 750 KB per
IHTMLDocument2::write/close. If I comment out the call to
m_spHtmlDoc->write(pSA);  the memory usage is more normal.

I am using below code to update the HTML Content.

IMPLEMENT_DYNCREATE(CEzHtmlChildDlg, CDHtmlDialog)

CEzHtmlChildDlg::CEzHtmlChildDlg(CWnd* pParent /*=NULL*/)
	: CDHtmlDialog(CEzHtmlChildDlg::IDD, 0, pParent)
{
	CDHtmlDialog::Create(CEzHtmlChildDlg::IDD, pParent);
}

CEzHtmlChildDlg::~CEzHtmlChildDlg()
{
}

void CEzHtmlChildDlg::DoDataExchange(CDataExchange* pDX)
{
	CDHtmlDialog::DoDataExchange(pDX);
}

BOOL CEzHtmlChildDlg::OnInitDialog()
{
	SetHostFlags(m_dwHostFlags
		| DOCHOSTUIFLAG_DIALOG					// MSHTML does not enable selection of the text in the form
		| DOCHOSTUIFLAG_DISABLE_HELP_MENU		// MSHTML does not add the Help menu item to the container's menu.
		| DOCHOSTUIFLAG_SCROLL_NO				// MSHTML does not have scroll bars.
		| DOCHOSTUIFLAG_DISABLE_SCRIPT_INACTIVE // MSHTML does not execute any script until fully activated.
		| DOCHOSTUIFLAG_THEME					// Specifies that the hosted browser should use themes for pages it displays
//		| DOCHOSTUIFLAG_NOTHEME
		);

	CDHtmlDialog::OnInitDialog();

	// disable drop targets
	// Don't allow user to drop shortcut onto this window and have the browser
	// navigate to show that web site
	m_pBrowserApp->put_RegisterAsDropTarget(VARIANT_FALSE);

	

	EnableAutomation();
	LPDISPATCH pDisp = GetIDispatch(FALSE);
	SetExternalDispatch(pDisp);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

BOOL CEzHtmlChildDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (pMsg->message == WM_KEYDOWN && 
		(pMsg->wParam == VK_ESCAPE || pMsg->wParam == VK_RETURN || pMsg->wParam == VK_F5))
		return true;

	if (pMsg->message == WM_RBUTTONDOWN || pMsg->message == WM_RBUTTONDBLCLK)
		return TRUE;
	
	return CDHtmlDialog::PreTranslateMessage(pMsg);
}

BEGIN_MESSAGE_MAP(CEzHtmlChildDlg, CDHtmlDialog)
END_MESSAGE_MAP()

BEGIN_DHTML_EVENT_MAP(CEzHtmlChildDlg)

END_DHTML_EVENT_MAP()

// CEzHtmlChildDlg message handlers

void CEzHtmlChildDlg::SetHTML(CString strHtml)
{
	// Create a safearray to store the HTML text
    SAFEARRAY      *pSA;
    SAFEARRAYBOUND  saBound = {1, 0};
    pSA = SafeArrayCreate(VT_VARIANT, 1, &saBound);

	 CComVariant varDummy;

    // Copy the HTML into the one and only element
    VARIANT   *pVar;
    CComBSTR   bstrHTML = strHtml;              // Load the text
    varDummy = bstrHTML;                        // .. into a variant

    SafeArrayAccessData(pSA, (void**)&pVar);    // Access safearray data
    pVar[0] = varDummy;                         // Set the text data
    SafeArrayUnaccessData(pSA);                 // Release access

	 // Write the HTML as the document's new text
    m_spHtmlDoc->write(pSA);                           // Overwrite HTML
    m_spHtmlDoc->close();                              // Update browser

    SafeArrayDestroy(pSA);                      // Finished with the safearray
}

m_spHtmlDoc  is a member variable of CDhtmlDialog.

What is the mistake I am doing while writing to document?




解决方案

IHTMLDocument2.write method says "Do not use the write method or the writeln method on the current document after the document has finished loading unless you first call the open method, which clears the current document window and erases all variables."

Just guessing, but maybe this is pertinent.


这篇关于IHTMLDocument2.write()memoryleak的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆