use weak to avoid use nativeEngine after destroy

Signed-off-by: zcdqs <junfeng.lijunfeng@huawei.com>
Change-Id: I0073eaebfdaaa728ad09b2778664ad48df388413
This commit is contained in:
zcdqs
2022-04-26 16:46:12 +08:00
parent 1de5dbfe8e
commit 0bfaf11b0e
@@ -758,17 +758,23 @@ void JsiDeclarativeEngine::SetPostTask(NativeEngine* nativeEngine)
{
LOGI("SetPostTask");
auto weakDelegate = AceType::WeakClaim(AceType::RawPtr(engineInstance_->GetDelegate()));
auto&& postTask = [weakDelegate, nativeEngine = nativeEngine_, id = instanceId_](bool needSync) {
auto&& postTask = [weakDelegate, weakEngine = AceType::WeakClaim(this), id = instanceId_](bool needSync) {
auto delegate = weakDelegate.Upgrade();
if (delegate == nullptr) {
LOGE("delegate is nullptr");
return;
}
delegate->PostJsTask([nativeEngine, needSync, id]() {
ContainerScope scope(id);
delegate->PostJsTask([weakEngine, needSync, id]() {
auto jsEngine = weakEngine.Upgrade();
if (jsEngine == nullptr) {
LOGW("jsEngine is nullptr");
return;
}
auto nativeEngine = jsEngine->GetNativeEngine();
if (nativeEngine == nullptr) {
return;
}
ContainerScope scope(id);
nativeEngine->Loop(LOOP_NOWAIT, needSync);
});
};