Delete assets after artboards to fix race condition with FileAssetReferencers.

I caught a random crash in Unity. Our unreferencing logic has a race condition! This is probably causing crashes in a lot of the runtimes, randomly.

We attempt to unregister a file asset referencer when the FileAssetReferencer is destructed here:
https://github.com/rive-app/rive/blob/d708e6a07b454ce0fc20d9432172f2b58c9f29f7/packages/runtime/src/assets/file_asset_referencer.cpp#L10-L13

This causes invalid memory access if m_fileAsset has already been deleted. This can happen when the source artboards stored in a file get deleted. Doesn't affect instances, but any-time a whole File is destructed this race condition can occur. We store our artboards and file assets as lists in our File object. Because they are stored in vectors as unique ptrs, we aren't guaranteed destructor order (some compilers will destruct the list of artboards first and some will destruct the files first). I've seen Clang do both (in Windows it was destroying the assets first which causes the problem as the references in the artboard then try to access the file assets).

We should be really cautious when accessing bare pointers in destructors and make sure we understand the lifecycle of any objects we're referencing during destruction.

For now this is the cleanest immediate fix, ensure that destruction is done in order:
1. Artboards and their components first
2. File assets
3. Backboard

We introduced this here: https://github.com/rive-app/rive/pull/6068

Diffs=
18ae32102 Delete assets after artboards to fix race condition with FileAssetReferencers. (#6223)

Co-authored-by: Luigi Rosso <luigi-rosso@users.noreply.github.com>
5 files changed
tree: 82e4c84917fdd552fded1b983eb91dacd9c03eb1
  1. .github/
  2. .vscode/
  3. build/
  4. cg_renderer/
  5. decoders/
  6. dependencies/
  7. dev/
  8. include/
  9. rivinfo/
  10. skia/
  11. src/
  12. tess/
  13. test/
  14. utils/
  15. viewer/
  16. .dockerignore
  17. .gitignore
  18. .lua-format
  19. .rive_head
  20. build.sh
  21. Dockerfile
  22. Doxyfile
  23. LICENSE
  24. README.md
README.md

Build Status Discord badge Twitter handle

rive-cpp

Rive hero image

Rive C++ is a runtime library for Rive, a real-time interactive design and animation tool.

The C++ runtime for Rive provides these runtime features:

  • Loading Artboards and their contents from .riv files.
  • Querying LinearAnimations and StateMachines from Artboards.
  • Making changes to Artboard hierarchy (fundamentally same guts used by LinearAnimations and StateMachines) and effienclty solving those changes via Artboard::advance.
  • Abstract Renderer for submitting high level vector path commands with retained path objects to optimize and minimize path re-computation (ultimately up to the concrete rendering implementation).
  • Example concrete renderer written in C++ with Skia. Skia renderer code is in skia/renderer/src/skia_renderer.cpp.

Build System

We use premake5. The Rive dev team primarily works on MacOS. There is some work done by the community to also support Windows and Linux. PRs welcomed for specific platforms you wish to support! We encourage you to use premake as it's highly extensible and configurable for a variety of platforms.

Build

In the rive-cpp directory, run build.sh to debug build and build.sh release for a release build.

If you've put the premake5 executable in the rive-cpp/build folder, you can run it with PATH=.:$PATH ./build.sh

Rive makes use of clang vector builtins, which are, as of 2022, still a work in progress. Please use clang and ensure you have the latest version.

Building skia projects

cd skia/dependencies
./make_skia.sh      // this will invoke get_skia.sh

To build viewer (plus you'll needed CMake installed)

./make_viewer_dependencies.sh

Testing

Uses the Catch2 testing framework.

cd dev
./test.sh

In the dev directory, run test.sh to compile and execute the tests.

(if you've installed premake5 in rive-cpp/build, you can run it with PATH=../../build:$PATH ./test.sh)

The tests live in rive/test. To add new tests, create a new xxx_test.cpp file here. The test harness will automatically pick up the new file.

There's a VSCode command provided to run tests from the Tasks: Run Task command palette.

Code Formatting

rive-cpp uses clang-format, you can install it with brew on MacOS: brew install clang-format.

Memory Checks

Note that if you‘re on MacOS you’ll want to install valgrind, which is somewhat complicated these days. This is the easiest solution (please PR a better one when it becomes available).

brew tap LouisBrunner/valgrind
brew install --HEAD LouisBrunner/valgrind/valgrind

You can now run the all the tests through valgrind by running test.sh memory.

Disassembly Explorer

If you want to examine the generated assembly code per cpp file, install Disassembly Explorer in VSCode.

A disassemble task is provided to compile and preview the generated assembly. You can reach it via the Tasks: Run Task command palette or you can bind it to a shortcut by editing your VSCode keybindings.json:

[
    {
        "key": "cmd+d",
        "command": "workbench.action.tasks.runTask",
        "args": "disassemble"
    }
]