From 8b9090eb556c68e92ae851b7068a8e34d7bca786 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 20 Jun 2013 22:51:54 +0800 Subject: [PATCH] fixed #2315: [JSB] Iterating through cc.Node children causes crash. --- cocos2dx/base_nodes/CCNode.cpp | 4 +- .../jsb_cocos2dx_extension_manual.cpp | 63 +++++++++---------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/cocos2dx/base_nodes/CCNode.cpp b/cocos2dx/base_nodes/CCNode.cpp index 95d4767ef6..5dd95f6d85 100644 --- a/cocos2dx/base_nodes/CCNode.cpp +++ b/cocos2dx/base_nodes/CCNode.cpp @@ -504,16 +504,16 @@ void Node::setGLServerState(ccGLServerState glServerState) void Node::setUserObject(Object *pUserObject) { - CC_SAFE_RELEASE(_userObject); CC_SAFE_RETAIN(pUserObject); + CC_SAFE_RELEASE(_userObject); _userObject = pUserObject; } void Node::setShaderProgram(GLProgram *pShaderProgram) { + CC_SAFE_RETAIN(pShaderProgram); CC_SAFE_RELEASE(_shaderProgram); _shaderProgram = pShaderProgram; - CC_SAFE_RETAIN(_shaderProgram); } Rect Node::boundingBox() diff --git a/scripting/javascript/bindings/jsb_cocos2dx_extension_manual.cpp b/scripting/javascript/bindings/jsb_cocos2dx_extension_manual.cpp index ae9088dac7..8d36bef1a3 100644 --- a/scripting/javascript/bindings/jsb_cocos2dx_extension_manual.cpp +++ b/scripting/javascript/bindings/jsb_cocos2dx_extension_manual.cpp @@ -14,7 +14,7 @@ USING_NS_CC; USING_NS_CC_EXT; class JSB_ScrollViewDelegate -: public Node +: public Object , public ScrollViewDelegate { public: @@ -82,12 +82,7 @@ static JSBool js_cocos2dx_CCScrollView_setDelegate(JSContext *cx, uint32_t argc, JSB_ScrollViewDelegate* nativeDelegate = new JSB_ScrollViewDelegate(); nativeDelegate->setJSDelegate(jsDelegate); - JSB_ScrollViewDelegate* oldDelegate = (JSB_ScrollViewDelegate*)cobj->getDelegate(); - if (oldDelegate) - { - oldDelegate->removeFromParent(); - } - cobj->addChild(nativeDelegate); + cobj->setUserObject(nativeDelegate); cobj->setDelegate(nativeDelegate); nativeDelegate->release(); @@ -99,8 +94,12 @@ static JSBool js_cocos2dx_CCScrollView_setDelegate(JSContext *cx, uint32_t argc, return JS_FALSE; } + +#define KEY_TABLEVIEW_DATA_SOURCE "TableViewDataSource" +#define KEY_TABLEVIEW_DELEGATE "TableViewDelegate" + class JSB_TableViewDelegate -: public Node +: public Object , public TableViewDelegate { public: @@ -206,13 +205,16 @@ static JSBool js_cocos2dx_CCTableView_setDelegate(JSContext *cx, uint32_t argc, JSB_TableViewDelegate* nativeDelegate = new JSB_TableViewDelegate(); nativeDelegate->setJSDelegate(jsDelegate); - - JSB_TableViewDelegate* oldDelegate = (JSB_TableViewDelegate*)cobj->getDelegate(); - if (oldDelegate) + Dictionary* userDict = static_cast(cobj->getUserObject()); + if (NULL == userDict) { - oldDelegate->removeFromParent(); + userDict = new Dictionary(); + cobj->setUserObject(userDict); + userDict->release(); } - cobj->addChild(nativeDelegate); + + userDict->setObject(nativeDelegate, KEY_TABLEVIEW_DELEGATE); + cobj->setDelegate(nativeDelegate); nativeDelegate->release(); @@ -225,7 +227,7 @@ static JSBool js_cocos2dx_CCTableView_setDelegate(JSContext *cx, uint32_t argc, } class JSB_TableViewDataSource -: public Node +: public Object , public TableViewDataSource { public: @@ -384,12 +386,16 @@ static JSBool js_cocos2dx_CCTableView_setDataSource(JSContext *cx, uint32_t argc JSB_TableViewDataSource* pNativeSource = new JSB_TableViewDataSource(); pNativeSource->setTableViewDataSource(JSVAL_TO_OBJECT(argv[0])); - JSB_TableViewDataSource* oldDataSource = (JSB_TableViewDataSource*)cobj->getDataSource(); - if (oldDataSource) + Dictionary* userDict = static_cast(cobj->getUserObject()); + if (NULL == userDict) { - oldDataSource->removeFromParent(); + userDict = new Dictionary(); + cobj->setUserObject(userDict); + userDict->release(); } - cobj->addChild(pNativeSource); + + userDict->setObject(pNativeSource, KEY_TABLEVIEW_DATA_SOURCE); + cobj->setDataSource(pNativeSource); pNativeSource->release(); @@ -417,12 +423,6 @@ static JSBool js_cocos2dx_CCTableView_create(JSContext *cx, uint32_t argc, jsval ret = new TableView(); ret->autorelease(); - JSB_TableViewDataSource* oldDataSource = (JSB_TableViewDataSource*)ret->getDataSource(); - if (oldDataSource) - { - oldDataSource->removeFromParent(); - } - ret->setDataSource(pNativeSource); jsval jsret; @@ -454,7 +454,11 @@ static JSBool js_cocos2dx_CCTableView_create(JSContext *cx, uint32_t argc, jsval } ret->reloadData(); - ret->addChild(pNativeSource); + Dictionary* userDict = new Dictionary(); + userDict->setObject(pNativeSource, KEY_TABLEVIEW_DATA_SOURCE); + ret->setUserObject(userDict); + userDict->release(); + pNativeSource->release(); JS_SET_RVAL(cx, vp, jsret); @@ -466,7 +470,7 @@ static JSBool js_cocos2dx_CCTableView_create(JSContext *cx, uint32_t argc, jsval } class JSB_EditBoxDelegate -: public Node +: public Object , public EditBoxDelegate { public: @@ -556,12 +560,7 @@ static JSBool js_cocos2dx_CCEditBox_setDelegate(JSContext *cx, uint32_t argc, js JSB_EditBoxDelegate* nativeDelegate = new JSB_EditBoxDelegate(); nativeDelegate->setJSDelegate(jsDelegate); - JSB_EditBoxDelegate* oldDelegate = (JSB_EditBoxDelegate*)cobj->getDelegate(); - if (oldDelegate) - { - oldDelegate->removeFromParent(); - } - cobj->addChild(nativeDelegate); + cobj->setUserObject(nativeDelegate); cobj->setDelegate(nativeDelegate); nativeDelegate->release();