下面代码中, 如果 this 不是Natvie Object,或者是null,则在退出程序时会crash在 ScriptingCore::cleanup()/JS_DestroyContext(_cx) 里。
var listView = ccui.ListView.create();
listView.addEventListenerListView(this.onItemSelected, );
大概看了一下,应该是JSStudioEventListenerWrapper::m_bNeedUnroot == true时,会出问题。
JSStudioEventListenerWrapper::~JSStudioEventListenerWrapper()
{
if (m_bNeedUnroot)
{
JSObject *thisObj = JSVAL_TO_OBJECT(_jsThisObj);
JSContext *cx = ScriptingCore::getInstance()->getGlobalContext();
JS_RemoveObjectRoot(cx, &thisObj);
}
}
void JSStudioEventListenerWrapper::setJSCallbackThis(jsval jsThisObj)
{
JSCallbackWrapper::setJSCallbackThis(jsThisObj);
JSObject *thisObj = JSVAL_TO_OBJECT(jsThisObj);
js_proxy *p = jsb_get_js_proxy(thisObj);
if (!p)
{
JSContext *cx = ScriptingCore::getInstance()->getGlobalContext();
JS_AddObjectRoot(cx, &thisObj);
m_bNeedUnroot = true;
}
}
我不太了解js本身在怎么工作的,所以也不知道具体原因。
目前解决方法是只用native obj当做this。但是没法用匿名函数了。