dispose method

void dispose()

Stops the service and releases the native callback.

Calls the C++ stop_service function, cancels all stream subscriptions, closes the StreamController, and closes the NativeCallable, allowing the Dart isolate to exit cleanly. Safe to call multiple times.

Implementation

void dispose() {
  if (_disposed) return;
  _disposed = true;
  stopService();
  // Nullify the C++ callback pointer before closing the NativeCallable.
  // stop_service() only sets a flag; the worker thread may still call
  // notify_cb() while the Dart side is tearing down. Passing nullptr makes
  // the C++ guard (if (notify_cb) notify_cb()) a safe no-op from that point.
  _setMessageCallback(nullptr);
  for (final sub in _subscriptions) {
    sub.cancel();
  }
  _subscriptions.clear();
  _messageController.close();
  _finalizer.detach(this);
  _callable?.close();
}