Skip to content

Room destructor does not remove FFI event listener, causing use-after-free #39

@Sqhh99

Description

@Sqhh99

Describe the bug
When a Room object is destroyed, the FFI event listener registered during Connect() is not removed. This causes subsequent callbacks to reference a destroyed object, leading to use-after-free crashes.
To Reproduce

  1. Connect to a room
  2. Destroy the Room object (or let it go out of scope)
  3. Reconnect with a new Room
  4. Crash occurs when FFI dispatches events to the old, destroyed listener

Expected behavior
The FFI listener should be removed in the Room destructor to prevent dangling callbacks.

I tried to fix this by:

  1. Storing the listener ID during Connect()
  2. Calling FfiClient::RemoveListener() in the destructor
  3. Adding a mechanism to wait for in-flight callbacks to complete before destruction

Problem encountered

If a user destroys the Room from within a RoomDelegate callback (e.g., in onDisconnected), the destructor will deadlock because it waits for the callback to finish, but the callback is waiting for the destructor to return.
I'm unsure how to handle this edge case properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions