Fix potential crash in Node::isChildOf (#12493)

This commit is contained in:
James Chen
2022-08-22 09:54:42 +08:00
committed by GitHub
parent ebc5088eba
commit e4cd83ec59
2 changed files with 13 additions and 5 deletions
+7 -5
View File
@@ -1063,11 +1063,11 @@ Object.defineProperty(nodeProto, '_parent', {
configurable: true,
enumerable: true,
get () {
return this._parentInternal;
this._parentRef = this._parentInternal;
return this._parentRef;
},
set (v) {
// jsb.registerNativeRef(v, this); // Root JSB object to avoid child node being garbage collected
this._parentInternal = v;
this._parentRef = this._parentInternal = v;
},
});
@@ -1075,10 +1075,11 @@ Object.defineProperty(nodeProto, 'parent', {
configurable: true,
enumerable: true,
get () {
return this.getParent();
this._parentRef = this.getParent();
return this._parentRef;
},
set (v) {
// jsb.registerNativeRef(v, this); // Root JSB object to avoid child node being garbage collected
this._parentRef = v;
this.setParent(v);
},
});
@@ -1263,6 +1264,7 @@ nodeProto._instantiate = function (cloned: Node, isSyncedNode: boolean) {
//
nodeProto._ctor = function (name?: string) {
this.__nativeRefs = {};
this._parentRef = null;
this.__jsb_ref_id = undefined;
this._iN$t = null;
this.__editorExtras__ = { editorOnly: true };
+6
View File
@@ -89,6 +89,12 @@ Node::Node(const ccstd::string &name) {
Node::~Node() {
CC_SAFE_DELETE(_eventProcessor);
if (!_children.empty()) {
// Reset children's _parent to nullptr to avoid dangerous pointer
for (const auto &child : _children) {
child->_parent = nullptr;
}
}
}
void Node::onBatchCreated(bool dontChildPrefab) {