Reduce memory leaks when switching credocuments

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.
pull/2814/head
poire-z 7 years ago committed by Frans de Jonge
parent 96ea2ece96
commit 495accfec9

@ -9,6 +9,9 @@ local ffi = require("ffi")
local lfs = require("libs/libkoreader-lfs") local lfs = require("libs/libkoreader-lfs")
local logger = require("logger") local logger = require("logger")
-- engine can be initialized only once, on first document opened
local engine_initialized = false
local CreDocument = Document:new{ local CreDocument = Document:new{
-- this is defined in kpvcrlib/crengine/crengine/include/lvdocview.h -- this is defined in kpvcrlib/crengine/crengine/include/lvdocview.h
SCROLL_VIEW_MODE = 0, SCROLL_VIEW_MODE = 0,
@ -16,7 +19,6 @@ local CreDocument = Document:new{
_document = false, _document = false,
_loaded = false, _loaded = false,
engine_initilized = false,
line_space_percent = 100, line_space_percent = 100,
default_font = G_reader_settings:readSetting("cre_font") or "Noto Serif", default_font = G_reader_settings:readSetting("cre_font") or "Noto Serif",
@ -47,7 +49,7 @@ function CreDocument:cacheInit()
end end
function CreDocument:engineInit() function CreDocument:engineInit()
if not self.engine_initilized then if not engine_initialized then
require "libs/libkoreader-cre" require "libs/libkoreader-cre"
-- initialize cache -- initialize cache
self:cacheInit() self:cacheInit()
@ -66,7 +68,7 @@ function CreDocument:engineInit()
end end
end end
self.engine_initilized = true engine_initialized = true
end end
end end

@ -28,6 +28,11 @@ function DocumentRegistry:getProvider(file)
end end
function DocumentRegistry:openDocument(file) 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 if not self.registry[file] then
local provider = self:getProvider(file) local provider = self:getProvider(file)
if provider ~= nil then if provider ~= nil then

Loading…
Cancel
Save