From e863e9b7d9033e31ee122822bb5f6c6843216bac Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 18 Aug 2017 11:55:12 +0800 Subject: [PATCH] Fix iOS closing app crash --- .../js-bindings/manual/ScriptingCore.cpp | 112 ++++++++++-------- .../js-bindings/manual/ScriptingCore.h | 38 +++++- .../js-bindings/manual/cocos2d_specifics.cpp | 5 - .../js-bindings/manual/cocos2d_specifics.hpp | 30 ----- .../manual/spidermonkey_specifics.h | 2 - .../proj.ios_mac/ios/AppController.mm | 11 +- .../proj.ios_mac/ios/AppController.mm | 11 +- .../proj.ios_mac/ios/AppController.mm | 11 +- 8 files changed, 126 insertions(+), 94 deletions(-) diff --git a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp index 3bfa44977c..fc0cd3d968 100644 --- a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp +++ b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp @@ -79,21 +79,24 @@ using namespace cocos2d; //static uint32_t s_nestedLoopLevel = 0; // server entry point for the bg thread -//static void serverEntryPoint(unsigned int port); +static void serverEntryPoint(unsigned int port); -std::unordered_map _js_global_type_map; -static std::unordered_map _native_js_global_map; +// global datas +// type name ~> jsb class type +std::unordered_map *_js_global_type_map = nullptr; +// native pointer ~> jsb proxy +static std::unordered_map *_native_js_global_map = nullptr; +// filename ~> JSScript +static std::unordered_map *_filename_script = nullptr; +// port ~> socket +static std::unordered_map ports_sockets; +// callback list for JSB API registration static std::vector registrationList; static char *_js_log_buf = nullptr; const char *ScriptingCore::EVENT_RESET = "scriptingcore_event_reset"; -// name ~> JSScript map -static std::unordered_map filename_script; -// port ~> socket map -static std::unordered_map ports_sockets; - static void cc_closesocket(int fd) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) @@ -414,8 +417,8 @@ GetBuildId(JS::BuildIdCharVector* buildId) void jsbWeakPointerCompartmentCallback(JSContext* cx, JSCompartment* comp, void* data) { - auto it_js = _native_js_global_map.begin(); - while (it_js != _native_js_global_map.end()) + auto it_js = _native_js_global_map->begin(); + while (it_js != _native_js_global_map->end()) { js_proxy_t *proxy = it_js->second; if (proxy->obj) @@ -435,11 +438,11 @@ on_garbage_collect(JSContext* cx, JSGCStatus status, void* data) * garbage collected. */ if (status == JSGC_BEGIN) { - printf("on_garbage_collect: begin, Native -> JS map count: %d\n", (int)_native_js_global_map.size()); + printf("on_garbage_collect: begin, Native -> JS map count: %d\n", (int)_native_js_global_map->size()); } else if (status == JSGC_END) { - printf("on_garbage_collect: end, Native -> JS map count: %d\n", (int)_native_js_global_map.size()); + printf("on_garbage_collect: end, Native -> JS map count: %d\n", (int)_native_js_global_map->size()); } } @@ -546,6 +549,11 @@ ScriptingCore::ScriptingCore() , _callFromScript(false) , _finalizing(nullptr) { + // Init global datas + _js_global_type_map = new (std::nothrow) std::unordered_map(); + _native_js_global_map = new (std::nothrow) std::unordered_map(); + _filename_script = new (std::nothrow) std::unordered_map(); + bool ok = JS_Init(); if (!ok) { CCLOG("System errno : %d", errno); @@ -629,8 +637,8 @@ void ScriptingCore::addRegisterCallback(sc_register_sth callback) { void ScriptingCore::removeAllProxys(JSContext *cx) { - auto it_js = _native_js_global_map.begin(); - while (it_js != _native_js_global_map.end()) + auto it_js = _native_js_global_map->begin(); + while (it_js != _native_js_global_map->end()) { // Erase hook private first in case gc access the proxy again auto proxy = it_js->second; @@ -648,9 +656,9 @@ void ScriptingCore::removeAllProxys(JSContext *cx) } CC_SAFE_DELETE(proxy); - it_js = _native_js_global_map.erase(it_js); + it_js = _native_js_global_map->erase(it_js); } - _native_js_global_map.clear(); + _native_js_global_map->clear(); } void ScriptingCore::createGlobalContext() { @@ -760,17 +768,17 @@ bool ScriptingCore::getScript(const std::string& path, JS::MutableHandleScript s { // a) check jsc file first std::string byteCodePath = RemoveFileExt(std::string(path)) + BYTE_CODE_FILE_EXT; - if (filename_script.find(byteCodePath) != filename_script.end()) + if (_filename_script->find(byteCodePath) != _filename_script->end()) { - script.set(filename_script[byteCodePath]->get()); + script.set((*_filename_script)[byteCodePath]->get()); return true; } // b) no jsc file, check js file std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(path); - if (filename_script.find(fullPath) != filename_script.end()) + if (_filename_script->find(fullPath) != _filename_script->end()) { - script.set(filename_script[fullPath]->get()); + script.set((*_filename_script)[fullPath]->get()); return true; } @@ -789,6 +797,8 @@ bool ScriptingCore::compileScript(const std::string& path, JS::HandleObject glob } cocos2d::FileUtils *futil = cocos2d::FileUtils::getInstance(); + bool isPopupNotify = futil->isPopupNotify(); + futil->setPopupNotify(false); JS::RootedObject obj(_cx, global); bool compileSucceed = false; @@ -808,10 +818,11 @@ bool ScriptingCore::compileScript(const std::string& path, JS::HandleObject glob if (appended && result == JS::TranscodeResult::TranscodeResult_Ok) { compileSucceed = true; - filename_script[byteCodePath] = new (std::nothrow) JS::PersistentRootedScript(_cx, script.get()); + (*_filename_script)[byteCodePath] = new (std::nothrow) JS::PersistentRootedScript(_cx, script.get()); } } } + futil->setPopupNotify(isPopupNotify); // b) no jsc file, check js file if (!compileSucceed) @@ -836,7 +847,7 @@ bool ScriptingCore::compileScript(const std::string& path, JS::HandleObject glob } if (ok) { compileSucceed = true; - filename_script[fullPath] = new (std::nothrow) JS::PersistentRootedScript(_cx, script.get()); + (*_filename_script)[fullPath] = new (std::nothrow) JS::PersistentRootedScript(_cx, script.get()); } } @@ -847,7 +858,7 @@ bool ScriptingCore::compileScript(const std::string& path, JS::HandleObject glob if (compileSucceed) { return true; } else { - LOGD("ScriptingCore:: compileScript fail:%s", path.c_str()); + LOGD("ScriptingCore::compileScript fail:%s", path.c_str()); return false; } } @@ -855,28 +866,28 @@ bool ScriptingCore::compileScript(const std::string& path, JS::HandleObject glob void ScriptingCore::cleanScript(const char *path) { std::string byteCodePath = RemoveFileExt(std::string(path)) + BYTE_CODE_FILE_EXT; - auto it = filename_script.find(byteCodePath); - if (it != filename_script.end()) + auto it = _filename_script->find(byteCodePath); + if (it != _filename_script->end()) { - filename_script.erase(it); + _filename_script->erase(it); } std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(path); - it = filename_script.find(fullPath); - if (it != filename_script.end()) + it = _filename_script->find(fullPath); + if (it != _filename_script->end()) { - filename_script.erase(it); + _filename_script->erase(it); } } -std::unordered_map& ScriptingCore::getFileScript() +std::unordered_map* ScriptingCore::getFileScript() { - return filename_script; + return _filename_script; } void ScriptingCore::cleanAllScript() { - filename_script.clear(); + _filename_script->clear(); } bool ScriptingCore::runScript(const std::string& path) @@ -956,6 +967,11 @@ ScriptingCore::~ScriptingCore() { cleanup(); JS_ShutDown(); + + // clear datas + CC_SAFE_DELETE(_js_global_type_map); + CC_SAFE_DELETE(_native_js_global_map); + CC_SAFE_DELETE(_filename_script); } void ScriptingCore::cleanup() @@ -973,12 +989,17 @@ void ScriptingCore::cleanup() cleanAllScript(); // Cleanup jsb type map - for (auto iter = _js_global_type_map.begin(); iter != _js_global_type_map.end(); ++iter) + for (auto iter = _js_global_type_map->begin(); iter != _js_global_type_map->end(); ++iter) { CC_SAFE_DELETE(iter->second->proto); free(iter->second); } - _js_global_type_map.clear(); + _js_global_type_map->clear(); + + // Release promise state + auto sc = getPromiseState(_cx); + sc->quitting = true; + delete sc; // force gc JS_GC(_cx); @@ -996,11 +1017,6 @@ void ScriptingCore::cleanup() free(_js_log_buf); _js_log_buf = nullptr; } - - // Release promise state - auto sc = getPromiseState(_cx); - sc->quitting = true; - delete sc; if (_cx) { @@ -1994,13 +2010,13 @@ js_proxy_t* jsb_new_proxy(JSContext *cx, void* nativeObj, JS::HandleObject jsObj // native to JS index proxy = new js_proxy_t(); CC_ASSERT(proxy && "not enough memory"); - CC_ASSERT(_native_js_global_map.find(nativeObj) == _native_js_global_map.end() && "Native Key should not be present"); + CC_ASSERT(_native_js_global_map->find(nativeObj) == _native_js_global_map->end() && "Native Key should not be present"); proxy->ptr = nativeObj; proxy->obj = jsObj.get(); // One Proxy in two entries - _native_js_global_map[nativeObj] = proxy; + (*_native_js_global_map)[nativeObj] = proxy; JS_SetPrivate(hookObj, proxy); } else CCLOG("jsb_new_proxy: Invalid keys"); @@ -2018,11 +2034,11 @@ js_proxy_t* jsb_bind_proxy(JSContext *cx, void* nativeObj, JS::HandleObject jsOb // native to JS index proxy = new js_proxy_t(); CC_ASSERT(proxy && "not enough memory"); - CC_ASSERT(_native_js_global_map.find(nativeObj) == _native_js_global_map.end() && "Native Key should not be present"); + CC_ASSERT(_native_js_global_map->find(nativeObj) == _native_js_global_map->end() && "Native Key should not be present"); proxy->ptr = nativeObj; proxy->obj = jsObj; - _native_js_global_map[nativeObj] = proxy; + (*_native_js_global_map)[nativeObj] = proxy; } else CCLOG("jsb_bind_proxy: Invalid keys"); return proxy; @@ -2030,8 +2046,8 @@ js_proxy_t* jsb_bind_proxy(JSContext *cx, void* nativeObj, JS::HandleObject jsOb js_proxy_t* jsb_get_native_proxy(void* nativeObj) { - auto search = _native_js_global_map.find(nativeObj); - if(search != _native_js_global_map.end()) + auto search = _native_js_global_map->find(nativeObj); + if(search != _native_js_global_map->end()) return search->second; return nullptr; } @@ -2076,10 +2092,10 @@ bool jsb_unbind_proxy(js_proxy_t* proxy) #endif // COCOS2D_DEBUG // delete entry in native proxy map - auto it_nat = _native_js_global_map.find(nativeKey); - if (it_nat != _native_js_global_map.end()) + auto it_nat = _native_js_global_map->find(nativeKey); + if (it_nat != _native_js_global_map->end()) { - _native_js_global_map.erase(it_nat); + _native_js_global_map->erase(it_nat); return true; } else diff --git a/native/cocos/scripting/js-bindings/manual/ScriptingCore.h b/native/cocos/scripting/js-bindings/manual/ScriptingCore.h index d47fa28b63..5a70e28eda 100644 --- a/native/cocos/scripting/js-bindings/manual/ScriptingCore.h +++ b/native/cocos/scripting/js-bindings/manual/ScriptingCore.h @@ -25,7 +25,6 @@ #ifndef __SCRIPTING_CORE_H__ #define __SCRIPTING_CORE_H__ - #include "jsapi.h" #include "jsfriendapi.h" @@ -42,6 +41,8 @@ #define ENGINE_VERSION "Cocos Creator v1.6" +extern std::unordered_map *_js_global_type_map; + void js_log(const char *format, ...); typedef void (*sc_register_sth)(JSContext* cx, JS::HandleObject global); @@ -322,7 +323,7 @@ public: * Gets the cached script objects for all executed js file * @return @~english The cached script object map */ - std::unordered_map& getFileScript(); + std::unordered_map* getFileScript(); /**@~english * Clean all script objects */ @@ -514,7 +515,7 @@ public: private: void string_report(JS::HandleValue val); - + public: int handleNodeEvent(void* data); int handleActionEvent(void* data); @@ -535,24 +536,49 @@ public: void restartVM(); }; +/** + * You don't need to manage the returned pointer. They live for the whole life of + * the app. + */ +template +inline js_type_class_t *js_get_type_from_native(T* native_obj) +{ + bool found = false; + std::string typeName = typeid(*native_obj).name(); + auto typeProxyIter = _js_global_type_map->find(typeName); + if (typeProxyIter == _js_global_type_map->end()) + { + typeName = typeid(T).name(); + typeProxyIter = _js_global_type_map->find(typeName); + if (typeProxyIter != _js_global_type_map->end()) + { + found = true; + } + } + else + { + found = true; + } + return found ? typeProxyIter->second : nullptr; +} + template js_type_class_t *jsb_register_class(JSContext *cx, JSClass *jsClass, JS::HandleObject proto) { js_type_class_t *p = nullptr; std::string typeName = TypeTest::s_name(); - if (_js_global_type_map.find(typeName) == _js_global_type_map.end()) + if (_js_global_type_map->find(typeName) == _js_global_type_map->end()) { p = (js_type_class_t *)malloc(sizeof(js_type_class_t)); memset(p, 0, sizeof(js_type_class_t)); p->jsclass = jsClass; p->proto = new JS::PersistentRootedObject(cx, proto); - _js_global_type_map.insert(std::make_pair(typeName, p)); + _js_global_type_map->insert(std::make_pair(typeName, p)); } return p; } - void handlePendingException(JSContext *cx); void make_class_extend(JSContext *cx, JS::HandleObject proto); diff --git a/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp b/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp index 9cee42752f..dfabe5fa64 100644 --- a/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp +++ b/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp @@ -430,11 +430,6 @@ bool js_cocos2dx_CCScene_init(JSContext *cx, uint32_t argc, JS::Value *vp) return false; } -js_type_class_t *js_get_type_from_node(cocos2d::Node* native_obj) -{ - return js_get_type_from_native(native_obj); -} - void js_add_FinalizeHook(JSContext *cx, JS::HandleObject target, bool isRef) { JS::RootedObject hook(cx); diff --git a/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp b/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp index 1dcf309fe9..a410d90428 100644 --- a/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp +++ b/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp @@ -233,36 +233,6 @@ protected: bool _isUpdateSchedule; }; -/** - * You don't need to manage the returned pointer. They live for the whole life of - * the app. - */ -template -inline js_type_class_t *js_get_type_from_native(T* native_obj) { - bool found = false; - std::string typeName = typeid(*native_obj).name(); - auto typeProxyIter = _js_global_type_map.find(typeName); - if (typeProxyIter == _js_global_type_map.end()) - { - typeName = typeid(T).name(); - typeProxyIter = _js_global_type_map.find(typeName); - if (typeProxyIter != _js_global_type_map.end()) - { - found = true; - } - } - else - { - found = true; - } - return found ? typeProxyIter->second : nullptr; -} - -/** -* get type from a cocos2d::Node, call function(js_get_type_from_native) above. -*/ -CC_JS_DLL js_type_class_t *js_get_type_from_node(cocos2d::Node* native_obj); - /** * Gets or creates a JSObject based on native_obj. * If native_obj is subclass of Ref, it will use the jsb_ref functions. diff --git a/native/cocos/scripting/js-bindings/manual/spidermonkey_specifics.h b/native/cocos/scripting/js-bindings/manual/spidermonkey_specifics.h index 1f863f2801..ba3b42b0aa 100644 --- a/native/cocos/scripting/js-bindings/manual/spidermonkey_specifics.h +++ b/native/cocos/scripting/js-bindings/manual/spidermonkey_specifics.h @@ -45,8 +45,6 @@ typedef struct js_type_class { JS::PersistentRootedObject *proto; } js_type_class_t; -extern std::unordered_map _js_global_type_map; - template< typename DERIVED > class TypeTest { diff --git a/native/templates/js-template-binary/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm b/native/templates/js-template-binary/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm index 8061f93565..2026158014 100755 --- a/native/templates/js-template-binary/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm +++ b/native/templates/js-template-binary/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm @@ -39,10 +39,14 @@ using namespace cocos2d; #pragma mark Application lifecycle // cocos2d application instance -static AppDelegate s_sharedApplication; +static AppDelegate *s_sharedApplication = nullptr; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + if (s_sharedApplication == nullptr) + { + s_sharedApplication = new AppDelegate(); + } cocos2d::Application *app = cocos2d::Application::getInstance(); // Initialize the GLView attributes @@ -127,6 +131,11 @@ static AppDelegate s_sharedApplication; Called when the application is about to terminate. See also applicationDidEnterBackground:. */ + if (s_sharedApplication) + { + delete s_sharedApplication; + s_sharedApplication = nullptr; + } } diff --git a/native/templates/js-template-default/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm b/native/templates/js-template-default/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm index 8061f93565..2026158014 100755 --- a/native/templates/js-template-default/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm +++ b/native/templates/js-template-default/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm @@ -39,10 +39,14 @@ using namespace cocos2d; #pragma mark Application lifecycle // cocos2d application instance -static AppDelegate s_sharedApplication; +static AppDelegate *s_sharedApplication = nullptr; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + if (s_sharedApplication == nullptr) + { + s_sharedApplication = new AppDelegate(); + } cocos2d::Application *app = cocos2d::Application::getInstance(); // Initialize the GLView attributes @@ -127,6 +131,11 @@ static AppDelegate s_sharedApplication; Called when the application is about to terminate. See also applicationDidEnterBackground:. */ + if (s_sharedApplication) + { + delete s_sharedApplication; + s_sharedApplication = nullptr; + } } diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm index 8061f93565..2026158014 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm +++ b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm @@ -39,10 +39,14 @@ using namespace cocos2d; #pragma mark Application lifecycle // cocos2d application instance -static AppDelegate s_sharedApplication; +static AppDelegate *s_sharedApplication = nullptr; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + if (s_sharedApplication == nullptr) + { + s_sharedApplication = new AppDelegate(); + } cocos2d::Application *app = cocos2d::Application::getInstance(); // Initialize the GLView attributes @@ -127,6 +131,11 @@ static AppDelegate s_sharedApplication; Called when the application is about to terminate. See also applicationDidEnterBackground:. */ + if (s_sharedApplication) + { + delete s_sharedApplication; + s_sharedApplication = nullptr; + } }