Skip to content

[rive_native] iOS crash in rive_native_plugin.mm when registerWithRegistrar is called multiple times #477

@lawinski

Description

@lawinski

Hi Rive team 👋

I've encountered a crash when using rive_native on iOS in combination with plugins like workmanager or home_widget. These plugins typically use setPluginRegistrantCallback to support multiple FlutterEngine instances (e.g. for background tasks).

This leads to multiple calls to registerWithRegistrar in rive_native_plugin.mm, and ultimately causes a crash:

std::unique_lock<std::mutex> lock(rt->_mutex); // <- EXC_BAD_ACCESS

Image

Each new call to registerWithRegistrar method replaces the existing instance which I guess leads to that issue.

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar
{
    FlutterMethodChannel* channel =
        [FlutterMethodChannel methodChannelWithName:@"rive_native"
                                    binaryMessenger:[registrar messenger]];
    riveNativePluginInstance =
        [[RiveNativePlugin alloc] initWithTextures:[registrar textures]];
    [registrar addMethodCallDelegate:riveNativePluginInstance channel:channel];
}

✅ Possible solution
Adding a guard to prevent reinitialization of the global riveNativePluginInstance solves the crash. However, I haven’t verified if this introduces any side effects in more complex setups. I just wanted to share my findings in case it's helpful or something you’d want to address in the plugin directly.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions