destroy method

  1. @override
Future<void> destroy()
override

Destroy this loopback context and release resources.

Implementation

@override
Future<void> destroy() async {
  // Idempotent - can be called multiple times safely
  if (_isDestroyed) {
    return; // Already destroyed
  }

  _isDestroyed = true; // Mark as destroyed first to prevent new operations

  await stopCapture(); // Stop capture if running

  if (_contextHandle != null) {
    _finalizer.detach(this); // Prevent finalizer from running
    final res = bindings.MiniAV_Loopback_DestroyContext(_contextHandle!);
    _contextHandle = null; // Clear the handle

    if (res != bindings.MiniAVResultCode.MINIAV_SUCCESS) {
      throw Exception('Failed to destroy loopback context: ${res.name}');
    }
  }
}