Skip to content

Commit 412d3a3

Browse files
authored
fix(🐛): Fix dangling pointer in requestRedraw (#2768)
1 parent ba1db84 commit 412d3a3

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

packages/skia/cpp/rnskia/RNSkPlatformContext.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ class RNSkPlatformContext {
4141
std::shared_ptr<react::CallInvoker> callInvoker,
4242
float pixelDensity)
4343
: _pixelDensity(pixelDensity), _jsRuntime(runtime),
44-
_callInvoker(callInvoker) {
45-
}
44+
_callInvoker(callInvoker) {}
4645

4746
virtual ~RNSkPlatformContext() = default;
4847

packages/skia/cpp/rnskia/RNSkView.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,17 @@ class RNSkView : public std::enable_shared_from_this<RNSkView> {
173173
void requestRedraw() {
174174
if (!_redrawRequested) {
175175
_redrawRequested = true;
176-
_platformContext->runOnMainThread([this]() {
177-
if (_renderer) {
178-
_renderer->renderImmediate(_canvasProvider);
179-
_redrawRequested = false;
176+
// Capture a weak pointer to this
177+
auto weakThis = std::weak_ptr<RNSkView>(shared_from_this());
178+
179+
_platformContext->runOnMainThread([weakThis]() {
180+
// Try to lock the weak pointer
181+
if (auto strongThis = weakThis.lock()) {
182+
// Only proceed if the object still exists
183+
if (strongThis->_renderer) {
184+
strongThis->_renderer->renderImmediate(strongThis->_canvasProvider);
185+
strongThis->_redrawRequested = false;
186+
}
180187
}
181188
});
182189
}

0 commit comments

Comments
 (0)