mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-19 14:52:40 +08:00
fix(v8): fix iterator invalidation when removing handled promises (#357)
* fix(v8): fix iterator invalidation when removing handled promises * fix(native): prevent iterator invalidation in callbacks and scheduler
This commit is contained in:
@@ -37,6 +37,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
|
||||
#include "application/ApplicationManager.h"
|
||||
#include "audio/include/AudioEngine.h"
|
||||
@@ -265,10 +266,11 @@ int AudioEngineImpl::play2d(const ccstd::string &filePath, bool loop, float volu
|
||||
|
||||
auto iter = _callbackMap.find(id);
|
||||
if (iter != _callbackMap.end()) {
|
||||
if (state == IAudioPlayer::State::OVER) {
|
||||
iter->second(id, filePath);
|
||||
}
|
||||
auto callback = std::move(iter->second);
|
||||
_callbackMap.erase(iter);
|
||||
if (state == IAudioPlayer::State::OVER) {
|
||||
callback(id, filePath);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -157,6 +157,7 @@ void Scheduler::schedule(const ccSchedulerFunc &callback, void *target, float in
|
||||
if (iter == _hashForTimers.end()) {
|
||||
element = ccnew HashTimerEntry();
|
||||
element->target = target;
|
||||
element->generation = ++_nextTimerGeneration;
|
||||
|
||||
_hashForTimers[target] = element;
|
||||
|
||||
@@ -368,10 +369,23 @@ void Scheduler::runFunctionsToBePerformedInCocosThread() {
|
||||
void Scheduler::update(float dt) {
|
||||
_updateHashLocked = true;
|
||||
|
||||
// Iterate over all the custom selectors
|
||||
HashTimerEntry *elt = nullptr;
|
||||
for (auto iter = _hashForTimers.begin(); iter != _hashForTimers.end();) {
|
||||
elt = iter->second;
|
||||
// Callbacks can rehash the map or remove and recreate a target. Snapshot
|
||||
// identities, not iterators or entry pointers; new entries start next frame.
|
||||
struct TimerIdentity {
|
||||
void *target;
|
||||
uint64_t generation;
|
||||
};
|
||||
ccstd::vector<TimerIdentity> targets;
|
||||
targets.reserve(_hashForTimers.size());
|
||||
for (const auto &entry : _hashForTimers) {
|
||||
targets.push_back({entry.first, entry.second->generation});
|
||||
}
|
||||
for (const auto &target : targets) {
|
||||
auto iter = _hashForTimers.find(target.target);
|
||||
if (iter == _hashForTimers.end() || iter->second->generation != target.generation) {
|
||||
continue;
|
||||
}
|
||||
auto *elt = iter->second;
|
||||
_currentTarget = elt;
|
||||
_currentTargetSalvaged = false;
|
||||
|
||||
@@ -396,14 +410,9 @@ void Scheduler::update(float dt) {
|
||||
|
||||
// only delete currentTarget if no actions were scheduled during the cycle (issue #481)
|
||||
if (_currentTargetSalvaged && _currentTarget->timers.empty()) {
|
||||
++iter;
|
||||
removeHashElement(_currentTarget);
|
||||
if (iter != _hashForTimers.end()) {
|
||||
++iter;
|
||||
}
|
||||
} else {
|
||||
++iter;
|
||||
}
|
||||
_currentTarget = nullptr;
|
||||
}
|
||||
|
||||
_updateHashLocked = false;
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
|
||||
@@ -278,6 +279,7 @@ private:
|
||||
struct HashTimerEntry {
|
||||
ccstd::vector<Timer *> timers;
|
||||
void *target;
|
||||
uint64_t generation;
|
||||
int timerIndex;
|
||||
Timer *currentTimer;
|
||||
bool currentTimerSalvaged;
|
||||
@@ -291,9 +293,10 @@ private:
|
||||
|
||||
// Used for "selectors with interval"
|
||||
ccstd::unordered_map<void *, HashTimerEntry *> _hashForTimers;
|
||||
uint64_t _nextTimerGeneration = 0;
|
||||
struct HashTimerEntry *_currentTarget = nullptr;
|
||||
bool _currentTargetSalvaged = false;
|
||||
// If true unschedule will not remove anything from a hash. Elements will only be marked for deletion.
|
||||
// True while timer entries are being updated.
|
||||
bool _updateHashLocked = false;
|
||||
|
||||
// Used for "perform Function"
|
||||
|
||||
@@ -1184,10 +1184,12 @@ void ScriptEngine::removeUnhandledPromise(v8::Local<v8::Promise> promise) {
|
||||
// Remove handled promises from the list
|
||||
SE_ASSERT(promise->GetIsolate() == _isolate, "Wrong isolate");
|
||||
auto *isolate = promise->GetIsolate();
|
||||
for (auto it = _unhandledPromises.begin(); it != _unhandledPromises.end(); ++it) {
|
||||
for (auto it = _unhandledPromises.begin(); it != _unhandledPromises.end();) {
|
||||
v8::Local<v8::Promise> const unhandledPromise = std::get<0>(*it).Get(isolate);
|
||||
if (unhandledPromise == promise) {
|
||||
_unhandledPromises.erase(it--);
|
||||
it = _unhandledPromises.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1007,7 +1007,8 @@ public:
|
||||
for (auto *set : sets) {
|
||||
set->update(oldView, newView);
|
||||
}
|
||||
_gpuBufferViewSet.erase(iter);
|
||||
// Connecting the new view may have rehashed the map.
|
||||
_gpuBufferViewSet.erase(oldView);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1018,7 +1019,8 @@ public:
|
||||
for (auto *set : sets) {
|
||||
set->update(oldView, newView);
|
||||
}
|
||||
_gpuTextureViewSet.erase(iter);
|
||||
// Connecting the new view may have rehashed the map.
|
||||
_gpuTextureViewSet.erase(oldView);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1217,7 +1219,8 @@ public:
|
||||
ia->update(oldBuffer, newBuffer);
|
||||
_ias[newBuffer].insert(ia);
|
||||
}
|
||||
_ias.erase(iter);
|
||||
// Registering the new buffer may have rehashed the map.
|
||||
_ias.erase(oldBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+18
-9
@@ -185,8 +185,9 @@ void GoogleBillingHelper::onBillingServiceDisconnected(JNIEnv* env, jclass clazz
|
||||
if (billingClient) {
|
||||
auto it = billingClient->_listeners.find(callbackId);
|
||||
if (it != billingClient->_listeners.end()) {
|
||||
cc::callJSfunc(it->second.get(), "onBillingServiceDisconnected");
|
||||
scopedListener listener(it->second.get());
|
||||
billingClient->_listeners.erase(it);
|
||||
cc::callJSfunc(listener.get(), "onBillingServiceDisconnected");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -203,8 +204,9 @@ void GoogleBillingHelper::onProductDetailsResponse(JNIEnv* env, jclass clazz,
|
||||
if (it != billingClient->_listeners.end()) {
|
||||
auto* billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
|
||||
std::vector<cc::ProductDetails*> productDetailsList = cc::JniBilling::toProductDetailList(env, productDetailsListObj, tag, startID);
|
||||
cc::callJSfunc(it->second.get(), "onProductDetailsResponse", billingResult, productDetailsList);
|
||||
scopedListener listener(it->second.get());
|
||||
billingClient->_listeners.erase(it);
|
||||
cc::callJSfunc(listener.get(), "onProductDetailsResponse", billingResult, productDetailsList);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -239,8 +241,9 @@ void GoogleBillingHelper::onConsumeResponse(JNIEnv* env, jclass clazz, jint tag,
|
||||
auto it = billingClient->_listeners.find(callbackId);
|
||||
if (it != billingClient->_listeners.end()) {
|
||||
auto* billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
|
||||
cc::callJSfunc(it->second.get(), "onConsumeResponse", billingResult, cc::StringUtils::getStringUTFCharsJNI(env, static_cast<jstring>(purchaseToken)));
|
||||
scopedListener listener(it->second.get());
|
||||
billingClient->_listeners.erase(it);
|
||||
cc::callJSfunc(listener.get(), "onConsumeResponse", billingResult, cc::StringUtils::getStringUTFCharsJNI(env, static_cast<jstring>(purchaseToken)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -256,8 +259,9 @@ void GoogleBillingHelper::onQueryPurchasesResponse(JNIEnv* env, jclass clazz, ji
|
||||
if (it != billingClient->_listeners.end()) {
|
||||
auto* billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
|
||||
std::vector<cc::Purchase*> purchasesList = cc::JniBilling::toPurchaseList(env, purchasesListObj, tag, startID);
|
||||
cc::callJSfunc(it->second.get(), "onQueryPurchasesResponse", billingResult, purchasesList);
|
||||
scopedListener listener(it->second.get());
|
||||
billingClient->_listeners.erase(it);
|
||||
cc::callJSfunc(listener.get(), "onQueryPurchasesResponse", billingResult, purchasesList);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -272,8 +276,9 @@ void GoogleBillingHelper::onBillingConfigResponse(JNIEnv* env, jclass clazz, jin
|
||||
if (billingConfigObj) {
|
||||
billingConfig = cc::JniBilling::toBillingConfig(env, billingConfigObj);
|
||||
}
|
||||
cc::callJSfunc(it->second.get(), "onBillingConfigResponse", billingResult, billingConfig);
|
||||
scopedListener listener(it->second.get());
|
||||
billingClient->_listeners.erase(it);
|
||||
cc::callJSfunc(listener.get(), "onBillingConfigResponse", billingResult, billingConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -288,8 +293,9 @@ void GoogleBillingHelper::onAlternativeBillingOnlyTokenResponse(JNIEnv* env, jcl
|
||||
if (alternativeBillingOnlyReportingDetailsObj) {
|
||||
toAlternativeBillingOnlyReporting = cc::JniBilling::toAlternativeBillingOnlyReportingDetails(env, alternativeBillingOnlyReportingDetailsObj);
|
||||
}
|
||||
cc::callJSfunc(it->second.get(), "onAlternativeBillingOnlyTokenResponse", billingResult, toAlternativeBillingOnlyReporting);
|
||||
scopedListener listener(it->second.get());
|
||||
billingClient->_listeners.erase(it);
|
||||
cc::callJSfunc(listener.get(), "onAlternativeBillingOnlyTokenResponse", billingResult, toAlternativeBillingOnlyReporting);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -304,8 +310,9 @@ void GoogleBillingHelper::onExternalOfferReportingDetailsResponse(JNIEnv* env, j
|
||||
if (externalOfferReportingDetailsObj) {
|
||||
externalOfferReportingDetails = cc::JniBilling::toExternalOfferReportingDetails(env, externalOfferReportingDetailsObj);
|
||||
}
|
||||
cc::callJSfunc(it->second.get(), "onExternalOfferReportingDetailsResponse", billingResult, externalOfferReportingDetails);
|
||||
scopedListener listener(it->second.get());
|
||||
billingClient->_listeners.erase(it);
|
||||
cc::callJSfunc(listener.get(), "onExternalOfferReportingDetailsResponse", billingResult, externalOfferReportingDetails);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -332,8 +339,9 @@ void GoogleBillingHelper::onInAppMessageResponse(JNIEnv* env, jclass clazz, jint
|
||||
auto it = billingClient->_listeners.find(callbackId);
|
||||
if (it != billingClient->_listeners.end()) {
|
||||
auto* inAppMessageResult = cc::JniBilling::toInAppMessageResult(env, inAppMessageResultObj);
|
||||
cc::callJSfunc(it->second.get(), "onInAppMessageResponse", inAppMessageResult);
|
||||
scopedListener listener(it->second.get());
|
||||
billingClient->_listeners.erase(it);
|
||||
cc::callJSfunc(listener.get(), "onInAppMessageResponse", inAppMessageResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -344,8 +352,9 @@ void GoogleBillingHelper::responseOnlyWithBillingResult(const std::string& funct
|
||||
auto it = billingClient->_listeners.find(callbackId);
|
||||
if (it != billingClient->_listeners.end()) {
|
||||
auto* billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
|
||||
cc::callJSfunc(it->second.get(), functionName.c_str(), billingResult);
|
||||
scopedListener listener(it->second.get());
|
||||
billingClient->_listeners.erase(it);
|
||||
cc::callJSfunc(listener.get(), functionName.c_str(), billingResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -178,7 +178,7 @@ void PlayTask::onTaskSuccess(int listerId, void* obj) {
|
||||
// Therefore, it should be removed first.
|
||||
scopedListener listener(it->second.get());
|
||||
_listeners.erase(it);
|
||||
callJSfuncWithJObject(it->second.get(), "onSuccess", obj);
|
||||
callJSfuncWithJObject(listener.get(), "onSuccess", obj);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user