From e8c94bf2264fb86d54f7e7b8601f7777eaf10825 Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Fri, 31 Mar 2023 10:11:47 +0200 Subject: [PATCH] Call assert in `SingleInstanceChecker` only in example apps (#4316) Currently, Reanimated crashes on some configurations due to an assert in `SingleInstanceChecker` which was meant to help us detect retain cycles and thus memory leaks in the library at an early stage but actually causes lots of troubles in client apps. This PR changes the behavior of `SingleInstanceChecker` so that it always prints the warning to the console but calls the failing `assert` only in Reanimated example apps (i.e. Example or FabricExample). This PR wraps the failing assert inside a preprocessor conditional based on the flags passed from `build.gradle` via `CMakeLists.txt` (Android) or `RNReanimated.podspec` (iOS). Additionally, it introduces `__android_log_print` instead of `std::cerr` on Android so that the warning is also visible in logcat. Android: android iOS: ios 1. Check if `IS_REANIMATED_EXAMPLE_APP` preprocessor definition is properly injected on both platforms: - Android: change implementation of `isReanimatedExampleApp` in build.gradle to always return `true` - iOS: assign `true` to `result[:is_reanimated_example_app]` in `reanimated_utils.rb` - add the following snippet in `SingleInstanceChecker.h`: ```cpp ``` 2. Check if the example apps crash on reload: - modify the condition in `SingleInstanceChecker.h` so that it always fails - the app should crash on launch 3. Check if the client apps don't crash on reload but show a warning: - modify the condition in `SingleInstanceChecker.h` so that it always fails - build the package locally - create a new RN app and install Reanimated - the app shouldn't crash on launch - there should be a warning in Android Logcat / iOS app output console --- Common/cpp/Tools/SingleInstanceChecker.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Common/cpp/Tools/SingleInstanceChecker.h b/Common/cpp/Tools/SingleInstanceChecker.h index 797afd829ca..af3e0c5aa2c 100644 --- a/Common/cpp/Tools/SingleInstanceChecker.h +++ b/Common/cpp/Tools/SingleInstanceChecker.h @@ -7,6 +7,10 @@ #include #include +#ifdef ANDROID +#include +#endif + namespace reanimated { // This is a class that counts how many instances of a different class there @@ -22,8 +26,12 @@ class SingleInstanceChecker { private: void assertWithMessage(bool condition, std::string message) { if (!condition) { - std::cerr << message << std::endl; - assert(condition); +#ifdef ANDROID + __android_log_print( + ANDROID_LOG_WARN, "Reanimated", "%s", message.c_str()); +#else + std::cerr << "[Reanimated] " << message << std::endl; +#endif } }