Solutions for memory leak problems when using twebbrowser (use SetProcessWorkingSetSize function, or modify Olectrls.pas source to solve problems)

Source: Internet
Author: User
Tags ole

With Twebbrower constantly open multiple Web pages, some versions of the operating system running for some time, found to occupy system memory up to hundreds of M, until the program is closed, the memory used to release.

This problem in the network has a lot of discussion, the more people's proposal is to use SetProcessWorkingSetSize (GetCurrentProcess (), -1,-1) to deal with, this flexible approach seems feasible.

Then there is a workaround in http://www.winu.cn/space-14160-do-blog-id-270.html, which mentions a memory leak in OLE and resolves the problem by overriding the OLE function.

Abandoned zhongs finally found the problem, without verification, plagiarism as follows:

Change Twebbrowser= Class (Tolecontrol) to twebbrowser= class (Trsolecontrol)

Unit rsolectrls;

Interface
Uses
Olectrls;

Type
Trsolecontrol = Class (Tolecontrol)
Protected
function Getidispatchprop (Index:integer): IDispatch;
function Getiunknownprop (Index:integer): IUnknown;
End

Implementation

{Tolecontrolfix}

function Trsolecontrol.getidispatchprop (Index:integer): IDispatch;
Var
Temp:tvardata;
Begin
GetProperty (Index, Temp);
Result: = IDispatch (Temp.vdispatch); Change to:
Pointer (Result): = Temp.vdispatch; This avoids the extra AddRef
End

function Trsolecontrol.getiunknownprop (Index:integer): IUnknown;
Var
Temp:tvardata;
Begin
GetProperty (Index, Temp);
Result: = IDispatch (Temp.vunknown); Change to:
Pointer (Result): = Temp.vunknown; This avoids the extra AddRef
End

End.

Another way to modify the source file is as follows:
Zhongs recommends adding an intermediate class to fix this problem in the middle of the inheritance relationship between Twebbrowser and Tolecontrol ...
But as I observed, I think Tolecontrol's flaws will spread to other existing and future automation controls, so my recommendation is to make the following modifications to Olectrls.pas:
function Tolecontrol.getidispatchprop (Index:integer): IDispatch;
Var
Temp:tvardata;
Begin
GetProperty (Index, Temp);
Result: = IDispatch (Temp.vdispatch);
Pointer (Result): = Temp.vdispatch;
End


function Tolecontrol.getiunknownprop (Index:integer): IUnknown;
Var
Temp:tvardata;
Begin
GetProperty (Index, Temp);
Result: = IUnknown (Temp.vunknown);
Pointer (Result): = Temp.vunknown;
End


And remember to recompile Olectrls.pas

RELATED links:
Http://blog.csdn.net/ScriptBaby/archive/2007/05/20/1618486.aspx
http://blog.csdn.net/ScriptBaby/article/details/1618486 http://blog.csdn.net/nhconch/article/details/7336192

Solutions for memory leak problems when using twebbrowser (use SetProcessWorkingSetSize function, or modify Olectrls.pas source to solve problems)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.