Skip to content

Commit

Permalink
Call assert in SingleInstanceChecker only in example apps (#4316)
Browse files Browse the repository at this point in the history
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:

<img width="1493" alt="android"
src="https://user-images.githubusercontent.com/20516055/229063204-36af61a7-1ace-4a64-bf43-4cb1caceb44f.png">

iOS:

<img width="898" alt="ios"
src="https://user-images.githubusercontent.com/20516055/229063251-d1b66d79-58ff-4077-a88c-f8d368d74377.png">

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
  • Loading branch information
tomekzaw committed Apr 21, 2023
1 parent dbdad98 commit e8c94bf
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Common/cpp/Tools/SingleInstanceChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <iostream>
#include <string>

#ifdef ANDROID
#include <android/log.h>
#endif

namespace reanimated {

// This is a class that counts how many instances of a different class there
Expand All @@ -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
}
}

Expand Down

0 comments on commit e8c94bf

Please sign in to comment.