From db9dcbddbc9231d520a14eaa4e256c276273eeb7 Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Wed, 26 Jul 2017 20:24:15 +0700 Subject: [PATCH] Forward refcount changes to NativeScriptInstance This also changes Reference::unreference() to always invoke refcount_decremented. Previously it was not invoked until the count reached zero due to short-circuit evalution of boolean expressions. --- core/reference.cpp | 3 ++- modules/nativescript/nativescript.cpp | 22 ++++++++++++++++++++++ modules/nativescript/nativescript.h | 3 +++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/core/reference.cpp b/core/reference.cpp index c55f8a7fe36..060608eacb2 100644 --- a/core/reference.cpp +++ b/core/reference.cpp @@ -74,7 +74,8 @@ bool Reference::unreference() { bool die = refcount.unref(); if (get_script_instance()) { - die = die && get_script_instance()->refcount_decremented(); + bool script_ret = get_script_instance()->refcount_decremented(); + die = die && script_ret; } return die; diff --git a/modules/nativescript/nativescript.cpp b/modules/nativescript/nativescript.cpp index fd83b747277..ae8746f687d 100644 --- a/modules/nativescript/nativescript.cpp +++ b/modules/nativescript/nativescript.cpp @@ -650,6 +650,28 @@ void NativeScriptInstance::notification(int p_notification) { call_multilevel("_notification", args, 1); } +void NativeScriptInstance::refcount_incremented() { + Variant::CallError err; + call("_refcount_incremented", NULL, 0, err); + if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) { + ERR_PRINT("Failed to invoke _refcount_incremented - should not happen"); + } +} + +bool NativeScriptInstance::refcount_decremented() { + Variant::CallError err; + Variant ret = call("_refcount_decremented", NULL, 0, err); + if (err.error != Variant::CallError::CALL_OK && err.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) { + ERR_PRINT("Failed to invoke _refcount_decremented - should not happen"); + return true; // assume we can destroy the object + } + if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) { + // the method does not exist, default is true + return true; + } + return ret; +} + Ref