From 412d3a35b2e10bba24fa27967321a94f7d9e387e Mon Sep 17 00:00:00 2001 From: William Candillon Date: Sun, 24 Nov 2024 21:02:13 +0100 Subject: [PATCH] =?UTF-8?q?fix(=F0=9F=90=9B):=20Fix=20dangling=20pointer?= =?UTF-8?q?=20in=20requestRedraw=20(#2768)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/skia/cpp/rnskia/RNSkPlatformContext.h | 3 +-- packages/skia/cpp/rnskia/RNSkView.h | 15 +++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/skia/cpp/rnskia/RNSkPlatformContext.h b/packages/skia/cpp/rnskia/RNSkPlatformContext.h index e64d6deef0..8303d327e0 100644 --- a/packages/skia/cpp/rnskia/RNSkPlatformContext.h +++ b/packages/skia/cpp/rnskia/RNSkPlatformContext.h @@ -41,8 +41,7 @@ class RNSkPlatformContext { std::shared_ptr callInvoker, float pixelDensity) : _pixelDensity(pixelDensity), _jsRuntime(runtime), - _callInvoker(callInvoker) { - } + _callInvoker(callInvoker) {} virtual ~RNSkPlatformContext() = default; diff --git a/packages/skia/cpp/rnskia/RNSkView.h b/packages/skia/cpp/rnskia/RNSkView.h index 85c30ec4f2..3c7f2ee451 100644 --- a/packages/skia/cpp/rnskia/RNSkView.h +++ b/packages/skia/cpp/rnskia/RNSkView.h @@ -173,10 +173,17 @@ class RNSkView : public std::enable_shared_from_this { void requestRedraw() { if (!_redrawRequested) { _redrawRequested = true; - _platformContext->runOnMainThread([this]() { - if (_renderer) { - _renderer->renderImmediate(_canvasProvider); - _redrawRequested = false; + // Capture a weak pointer to this + auto weakThis = std::weak_ptr(shared_from_this()); + + _platformContext->runOnMainThread([weakThis]() { + // Try to lock the weak pointer + if (auto strongThis = weakThis.lock()) { + // Only proceed if the object still exists + if (strongThis->_renderer) { + strongThis->_renderer->renderImmediate(strongThis->_canvasProvider); + strongThis->_redrawRequested = false; + } } }); }