Page 1 of 1

Loading local html files

Posted: Sun Jan 14, 2018 11:18 pm
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

Re: Loading local html files

Posted: Mon Jan 15, 2018 8:56 am
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.

Re: Loading local html files

Posted: Mon Jan 15, 2018 11:42 pm
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

Re: Loading local html files

Posted: Tue Jan 16, 2018 7:38 am
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;