Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Loading local html files

Post Reply
PioPio
Posts: 42
Joined: Sun Nov 05, 2017 10:25 pm

Loading local html files

Post by PioPio »

Hello,

I apologise in advance for making so many questions these days but I am very new to CEF4Delphi.

When I upload a local .html file via

Code: Select all

.LoadURL('file:///filename.html');
I have memory leaks when I close the application. I have no memory leaks if I load the same page from the web.

I looked the suggestion here https://www.briskbard.com/forum/viewtop ... dad52#p961 but this does not apply my case because what I want to upload the complete page and not only the html.

Is there anything I should consider when loading local files to avoid memory leaks?

Many thanks
Alberto
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Loading local html files

Post by salvadordf »

Hi,

I added FastMM4 to the uses clause in the simplebrowser2 and I didn't get memory leaks when I loaded a local html file.

If you search in google you will find that FastMM4 sometimes gives false positives, that is a false memory leak or memory access error.
However, I frequently use FastMM4 because it reports what was leaked and where was created.

Please, download FastMM4 from this address :
https://github.com/pleriche/FastMM4

Use it in your app and then copy the log file in this forum to see what could be the problem.
PioPio
Posts: 42
Joined: Sun Nov 05, 2017 10:25 pm

Re: Loading local html files

Post by PioPio »

Hi Salvador,

In the end I found out the root cause of the memory leak.

My application was going through an infinite loop and in the end it was halted. The halt procedure didn't freed up the classes and that caused the memory leak.
However, the root cause was infinite memory loop caused by the code in TChromium.OnLoadEnd that was not working properly when the html loaded is from a local file. I have changed my source into:

Code: Select all

procedure TForm1.Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser;
  const frame: ICefFrame; httpStatusCode: Integer);
begin
  FChromiumPageLoaded := False;
  if frame = nil then
    exit;

  if Pos('file:///', Chromium1.DocumentURL) <> 0 then//this is the part that manages local files
  begin
    if httpStatusCode = -3 then
    begin
      if (browser <> nil) then
      begin
        //page is fully loaded now
      end;
    end;
  end
  else
  begin// this part manages html from the web
    if httpStatusCode = 200 then
    begin
      if (browser <> nil) and (frame.IsMain = True) then
      begin
        //page is fully loaded now
      end;
    end;
  end;
end;
It seems it is working even if I am not sure this is the best way to handle it.

Alberto
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Loading local html files

Post by salvadordf »

Hi,

If you want to check for errors I would use the TChromium.OnLoadError event.
The errorCode parameter in that event gives more information.
errorCode can have all the ERR_* values defined in uCEFConstants.pas.

If you just want to know when the document has finished loading I would only check frame.IsMain in the TChromium.OnLoadEnd event.

Code: Select all

procedure TForm1.Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; httpStatusCode: Integer);
begin
    if (frame <> nil) and frame.IsMain then
      begin
        //page is fully loaded now
      end;
end;
Post Reply