Apparently there is a known problem with the component's Document property
For reference to anyone seeing this:
RSP-32393: Reference leak in TOleControl.GetIDispatchProp and TOleControl.GetIUnknownProp
UPDATE: this issue was reportedly fixed in 10.0 Seattle, so it should not
be happening anymore in 10.3.
I really don't want to try to fix the VCL, and the other suggestion of calling Release
might work if I knew where and how to do it.
You would call it like this:
function TMain.GetWebBrowserHTML(const WebBrowser: TWebBrowser): String;
var
Disp: IDispatch;
LStream: TStringStream;
Stream: IStream;
LPersistStreamInit: IPersistStreamInit;
begin
Disp := WebBrowser.Document;
if not Assigned(Disp) then
Exit;
try
LStream := TStringStream.Create('');
try
LPersistStreamInit := Disp as IPersistStreamInit;
Stream := TStreamAdapter.Create(LStream, soReference);
LPersistStreamInit.Save(Stream, True);
Result := LStream.DataString;
finally
LStream.Free;
end;
finally
Disp._Release;
end;
end;
Thus:
- the
TWebBrowser.Document
property returns an IDispatch
whose refcount has been erroneously incremented +2 instead of +1 due to a bug in TOleControl
- the assignment to
Disp
increments the refcount +1
- the cast+assignment to
LPersistStreamInit
increments the refcount +1.
When the function exits:
- the explicit
_Release()
decrements the refcount -1 to workaround the bug
- an implicit
_Release()
when LPersistStreamInit
goes out of scope decrements the refcount -1
- an implicit
_Release()
when Disp
goes out of scope decrements the refcount -1
- an implicit
_Release()
on the Document
property's return value decrements the refcount -1.
The refcount is balanced properly.
Alternatively, you can do this instead:
function TMain.GetWebBrowserHTML(const WebBrowser: TWebBrowser): String;
var
Disp: IDispatch;
LStream: TStringStream;
Stream: IStream;
LPersistStreamInit: IPersistStreamInit;
begin
Pointer(Disp) := WebBrowser.Document;
if not Assigned(Disp) then
Exit;
LStream := TStringStream.Create('');
try
LPersistStreamInit := Disp as IPersistStreamInit;
Stream := TStreamAdapter.Create(LStream, soReference);
LPersistStreamInit.Save(Stream, True);
Result := LStream.DataString;
finally
LStream.Free;
end;
end;
This way, you don't need the explicit _Release()
anymore:
- the
TWebBrowser.Document
property still returns an IDispatch
whose refcount has been erroneously incremented +2 instead of +1
- the assignment to
Disp
WON'T increment the refcount +1
- the cast+assignment to
LPersistStreamInit
increments the refcount +1.
When the function exits:
- an implicit
_Release()
when LPersistStreamInit
goes out of scope decrements the refcount -1
- an implicit
_Release()
when Disp
goes out of scope decrements the refcount -1
- an implicit
_Release()
on the Document
property's return value decrements the refcount -1.
The refcount is balanced properly.