Skip to content

Commit

Permalink
Reduce memory leaks when switching credocuments
Browse files Browse the repository at this point in the history
CRE cache, hyphdict and fonts can be initialized only once
when first credocument is opened. Previously, they were
recreated for each document, and as previous instances were probably
not free'd, this caused memory leaks.
  • Loading branch information
poire-z authored and Frenzie committed Apr 26, 2017
1 parent 96ea2ec commit 495accf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 deletions frontend/document/credocument.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ local ffi = require("ffi")
local lfs = require("libs/libkoreader-lfs")
local logger = require("logger")

-- engine can be initialized only once, on first document opened
local engine_initialized = false

local CreDocument = Document:new{
-- this is defined in kpvcrlib/crengine/crengine/include/lvdocview.h
SCROLL_VIEW_MODE = 0,
PAGE_VIEW_MODE = 1,

_document = false,
_loaded = false,
engine_initilized = false,

line_space_percent = 100,
default_font = G_reader_settings:readSetting("cre_font") or "Noto Serif",
Expand Down Expand Up @@ -47,7 +49,7 @@ function CreDocument:cacheInit()
end

function CreDocument:engineInit()
if not self.engine_initilized then
if not engine_initialized then
require "libs/libkoreader-cre"
-- initialize cache
self:cacheInit()
Expand All @@ -66,7 +68,7 @@ function CreDocument:engineInit()
end
end

self.engine_initilized = true
engine_initialized = true
end
end

Expand Down
5 changes: 5 additions & 0 deletions frontend/document/documentregistry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ function DocumentRegistry:getProvider(file)
end

function DocumentRegistry:openDocument(file)
-- force a GC, so that any previous document used memory can be reused
-- immediately by this new document without having to wait for the
-- next regular gc. The second call may help reclaming more memory.
collectgarbage()
collectgarbage()
if not self.registry[file] then
local provider = self:getProvider(file)
if provider ~= nil then
Expand Down

0 comments on commit 495accf

Please sign in to comment.