使用C++实现SDK之WebBrowser容器 系列的一个补充. 关于GetHtmlDocumentX()

来源:互联网 发布:淘宝量子统计 编辑:程序博客网 时间:2024/05/17 22:47

之前的文章 "http://blog.csdn.net/norsd/archive/2008/09/13/2921389.aspx" 实现了一个WebBrowser.

 

但是万物总不是完美的, 最近有网友问, GetHTMLDocumentX 系列函数的实现,是否需要Release.

 

当时我回答不需要. 我是这样考虑的:

 

WebBrowser 类只提供一个(系列)  IHtmlDocumentX* 指针, 在析构的时候 Release. 这样的实现听起来不错,实际运行也似乎没有问题.

 

非常巧合,最近终于发现问题.

 

考虑这样的情况:

 

 

1.打开页面  nothere.html

2.如果这个页面无法打开,WebBrowser会不断重试

3.这时候如果立刻跳转到其他页面,这时候WebBrowser会产生一个新的 IDocument 对象

 

这时候我们的 IHtmlDocumentX* 指针指向的还是之前的 nothere.html

只有我们 get_Document 才能得到当前页面的 document.

 

所以代码应该改成这样:

p.s: IHTMLDcoumentX  , 表示 IHTMLDocument , IHTMLDocument2 , and so on.