From 2f2bba5094feefd1814873dd0d7ac901ab740dd4 Mon Sep 17 00:00:00 2001 From: Emil Axelsson Date: Tue, 12 Dec 2017 18:55:44 +0100 Subject: [PATCH] Clear lua refs when unloading asset --- src/scene/assetloader.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/scene/assetloader.cpp b/src/scene/assetloader.cpp index fb61fc7ce2..a6cd04ed6c 100644 --- a/src/scene/assetloader.cpp +++ b/src/scene/assetloader.cpp @@ -235,6 +235,29 @@ bool AssetLoader::loadAsset(std::shared_ptr asset) { void AssetLoader::unloadAsset(std::shared_ptr asset) { tearDownAssetLuaTable(asset.get()); + for (int ref : _onInitializationFunctionRefs[asset.get()]) { + luaL_unref(*_luaState, LUA_REGISTRYINDEX, ref); + } + _onInitializationFunctionRefs.clear(); + + for (int ref : _onDeinitializationFunctionRefs[asset.get()]) { + luaL_unref(*_luaState, LUA_REGISTRYINDEX, ref); + } + _onDeinitializationFunctionRefs.clear(); + + for (const auto it : _onDependencyInitializationFunctionRefs[asset.get()]) { + for (int ref : it.second) { + luaL_unref(*_luaState, LUA_REGISTRYINDEX, ref); + } + } + _onDependencyInitializationFunctionRefs[asset.get()].clear(); + + for (const auto it : _onDependencyDeinitializationFunctionRefs[asset.get()]) { + for (int ref : it.second) { + luaL_unref(*_luaState, LUA_REGISTRYINDEX, ref); + } + } + _onDependencyDeinitializationFunctionRefs[asset.get()].clear(); } std::string AssetLoader::generateAssetPath(const std::string& baseDirectory,