0

My C++Builder application (RAD Studio XE4) is leaking memory. In doing some debugging, I tracked down the cause to the TWebBrowser component on a form that's created and destroyed; apparently, the TWebBrowser or one of the objects it creates is leaking memory.

I've also seem some strange access violations: if this form with its TWebBrowser has ever been created, and if a modal dialog is currently open, then a Group Policy refresh (whether from Windows' periodic background updates or from calling gpupdate) causes an access violation. Presumably the incompletely deleted TWebBrowser is trying to reapply some Internet Explorer settings and chasing a pointer to a deleted object.

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246

1 Answers1

3

This is a bug in C++Builder and in its handling of DelphiInterface properties. I've observed this in XE4 and XE6; other versions are untested.

Apparently this is the same issue that's described at QC#106829; Delphi's handling of IDispatch properties calls AddRef without properly calling Release.

A workaround is to avoid the IDispatch Document property and instead use the protected getter to get the document:

_di_IDispatch GetDocument(TWebBrowser *browser)
{
  // Hack to change access modifiers on TWebBrowser.  Since accessing the public
  // property doesn't work, we'll access the protected getter.
  class TWebBrowserHack : public TWebBrowser
  {
  public:
    _di_IDispatch GetDocument() { return Get_Document(); }
  };

  return static_cast<TWebBrowserHack*>(browser)->GetDocument();
}
Josh Kelley
  • 56,064
  • 19
  • 146
  • 246