From 3541320cd215cfa3053eba8e8b6bfa4c25524610 Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Mon, 8 May 2017 00:43:34 -0700 Subject: [PATCH] goodreads(fix): double free --- frontend/ui/widget/bookstatuswidget.lua | 2 ++ frontend/ui/widget/infomessage.lua | 7 ++++++- plugins/goodreads.koplugin/goodreadsbook.lua | 6 ++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/frontend/ui/widget/bookstatuswidget.lua b/frontend/ui/widget/bookstatuswidget.lua index 2d3b06a7c..417d39d52 100644 --- a/frontend/ui/widget/bookstatuswidget.lua +++ b/frontend/ui/widget/bookstatuswidget.lua @@ -301,6 +301,8 @@ function BookStatusWidget:genBookInfoGroup() width = img_width, height = img_height, }) + -- dereference thumbnail since we let imagewidget manages its lifecycle + self.thumbnail = nil end table.insert(book_info_group, CenterContainer:new{ diff --git a/frontend/ui/widget/infomessage.lua b/frontend/ui/widget/infomessage.lua index 554a50e3d..74873d491 100644 --- a/frontend/ui/widget/infomessage.lua +++ b/frontend/ui/widget/infomessage.lua @@ -47,7 +47,9 @@ local InfoMessage = InputContainer:new{ timeout = nil, -- in seconds width = nil, -- The width of the InfoMessage. Keep it nil to use default value. height = nil, -- The height of the InfoMessage. If this field is set, a scrollbar may be shown. - image = nil, -- The image shows at the left of the InfoMessage. + -- The image shows at the left of the InfoMessage. Image data will be freed + -- by InfoMessage, caller should not manage its lifecycle + image = nil, image_width = nil, -- The image width if image is used. Keep it nil to use original width. image_height = nil, -- The image height if image is used. Keep it nil to use original height. -- Whether the icon should be shown. If it is false, self.image will be ignored. @@ -76,6 +78,9 @@ function InfoMessage:init() local image_widget if self.show_icon then + -- TODO: remove self.image support, only used in filemanagersearch + -- this requires self.image's lifecycle to be managed by ImageWidget + -- instead of caller, which is easy to introduce bugs if self.image then image_widget = ImageWidget:new{ image = self.image, diff --git a/plugins/goodreads.koplugin/goodreadsbook.lua b/plugins/goodreads.koplugin/goodreadsbook.lua index b09c70550..5e59cb688 100644 --- a/plugins/goodreads.koplugin/goodreadsbook.lua +++ b/plugins/goodreads.koplugin/goodreadsbook.lua @@ -190,17 +190,19 @@ function GoodreadsBook:genBookInfoGroup() align = "top", HorizontalSpan:new{ width = split_span_width } } - --thumbnail + -- thumbnail local http = require("socket.http") local body = http.request(self.dates.image) local image = false if body then image = Pic.openJPGDocumentFromMem(body) end if image then table.insert(book_info_group, ImageWidget:new{ - image = image.image_bb, + image_disposable = false, + image = image.image_bb:copy(), width = img_width, height = img_height, }) + image:close() else table.insert(book_info_group, ImageWidget:new{ file = "resources/goodreadsnophoto.png",