From e748831f3ab6e8a94c1300437bc135d24631cbd4 Mon Sep 17 00:00:00 2001 From: Alberto Torres Date: Thu, 29 Aug 2024 00:13:37 +0200 Subject: [PATCH] Fix crash with loadables in Android. Warn if `done()` is being called twice. --- libs/loadable/loadable.nim | 5 +++++ src/loaders/blend.nim | 21 ++++++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/libs/loadable/loadable.nim b/libs/loadable/loadable.nim index 62ebeae..3db1886 100644 --- a/libs/loadable/loadable.nim +++ b/libs/loadable/loadable.nim @@ -281,9 +281,14 @@ proc loadUri*( if not is_remote: start_func = proc(self: LoadableResource) = + when not defined(release): + var done_called = false try: var memfile = memfiles.open(uri, mode=fmRead) self.done_func = proc() = + when not defined(release): + assert not done_called, "Done is being called multiple times. Did you forget to set auto_done = false?" + done_called = true memfile.close() self.onload(true, "", memfile.mem, memfile.size) # TODO!!!! check whether these objects are freed diff --git a/src/loaders/blend.nim b/src/loaders/blend.nim index 0930d1f..e05975b 100644 --- a/src/loaders/blend.nim +++ b/src/loaders/blend.nim @@ -88,14 +88,9 @@ proc registerBlendLoader*(engine: MyouEngine) = e.newBlendLoader() ) -var used_resources: seq[LoadableResource] - method close*(self: BlendLoader) = if self.resource != nil: self.resource.done() - # TODO: investigate - # this is a hack to avoid a crasn on android - used_resources.add self.resource self.resource = nil self.blend_file = nil self.cached_materials.clear() @@ -111,12 +106,16 @@ proc loadAsync(self: BlendLoader, callback: proc()) = callback() else: self.close() - self.resource = self.blend_file_path.loadUri proc(ok, err, data, len: auto) = - if ok: - self.blend_file = openBlendFile(self.blend_file_path, data, len) - callback() - else: - raise IOError.newException err + self.resource = loadUri(self.blend_file_path, + proc(ok, err, data, len: auto) = + if ok: + self.blend_file = openBlendFile(self.blend_file_path, data, len) + callback() + else: + raise IOError.newException err + , + auto_done = false + ) type BlenderObTypes = enum BEmpty = 0