mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
Fix potential crash in Node::isChildOf (#12493)
This commit is contained in:
@@ -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 };
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user