Skip to content

Commit 8f52f9a

Browse files
committed
fix: free demangled type names in single instance checker
1 parent df89d77 commit 8f52f9a

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

packages/react-native-reanimated/Common/cpp/reanimated/Tools/SingleInstanceChecker.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <atomic>
1010
#include <cassert>
11+
#include <cstdlib>
1112
#include <string>
1213

1314
#ifdef ANDROID
@@ -51,7 +52,10 @@ class SingleInstanceChecker {
5152
template <class T>
5253
SingleInstanceChecker<T>::SingleInstanceChecker() {
5354
int status = 0;
54-
std::string className = __cxxabiv1::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status);
55+
char *demangled = __cxxabiv1::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status);
56+
std::string className =
57+
(status == 0 && demangled != nullptr) ? demangled : typeid(T).name();
58+
std::free(demangled);
5559

5660
// React Native can spawn up to two instances of a Native Module at the same
5761
// time. This happens during a reload when a new instance of React Native is

packages/react-native-worklets/Common/cpp/worklets/Tools/SingleInstanceChecker.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <atomic>
1010
#include <cassert>
11+
#include <cstdlib>
1112
#include <iostream>
1213
#include <string>
1314

@@ -50,7 +51,10 @@ class SingleInstanceChecker {
5051
template <class T>
5152
SingleInstanceChecker<T>::SingleInstanceChecker() {
5253
int status = 0;
53-
std::string className = __cxxabiv1::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status);
54+
char *demangled = __cxxabiv1::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status);
55+
std::string className =
56+
(status == 0 && demangled != nullptr) ? demangled : typeid(T).name();
57+
std::free(demangled);
5458

5559
// React Native can spawn up to two instances of a Native Module at the same
5660
// time. This happens during a reload when a new instance of React Native is

0 commit comments

Comments
 (0)