--- This article was not analyzed in depth before it was written. It is now considered to be narrow (due to the bug of emwbbrowser, it is an error of RTL .). Please refer to subsequent articles ---
Because webbrowser is an ActiveX control based on the COM technology, and Delphi does not provide satisfactory support for com, there must be one or more exceptions when using twebbrowser and its derived objects, the error message is probably: "You didn't follow God's will to use webbrowser ". Today, I encountered a strange exception when releasing webbrowser.
The Code is as follows:
1 VaR
2 document: ihtmldocument2;
3url: widestring;
4 begin
5 Document = browser. Document as ihtmldocument2;
6url: = Document. url;
7 showmessage (URL );
8browser. Free;
9end;
10
After the above code is executed, an AV error will be reported. After tracking the RTL Code, the following error occurs:
Code
1 function _ intfclear (var dest: iinterface): pointer;
2 {$ ifdef purepascal}
3var
4 P: pointer;
5 begin
6 result: = @ DEST;
7if DEST <> nil then
8 begin
9 P: = pointer (DEST );
10 pointer (DEST): = nil;
11 iinterface (P). _ release;
12end;
13end;
14 {$ else}
15asm
16mov edX, [eax]
17 test edX, EDX
18je @ 1
19mov dword ptr [eax], 0
20 push eax
21 push edX
22mov eax, [edX]
23 call dword ptr [eax] + vmtoffset iinterface. _ release // The error is returned here.
24pop eax
25 @ 1:
26end;
27 {$ endif}
In addition, this code is generated after the end statement and the underlying RTL releases the iinterface interface. Because webbrowser has called the free method, the ihtmldocument2 pointed to by document has been released. RTL routinely releases the local variables of the function after each function is executed. Once a document is executed, it is released. As a result, the AV exception broke out.
You may want to take document: = nil; to avoid this operation without God's will. Unfortunately, this is not acceptable. The error code is as follows:
1 VaR
2 document: ihtmldocument2;
3url: widestring;
4 begin
5 Document = browser. Document as ihtmldocument2;
6url: = Document. url;
7 showmessage (URL );
8 Document: = nil;
9browser. Free;
10end;
11
To solve this problem, I have found only one method, that is, direct as transformation without declaring local variables. The Code is as follows:
1 VaR
2 URL: widestring;
3 begin
4 URL: = (browser. Document as ihtmldocument2). url;
5 showmessage (URL );
6 browser. Free;
7 end;
8
This can solve the AV error, but will make the code extremely bad.