ultralytics/yolo-flutter-app

controller.onDispose Not Working Properly

nikhilgarala-tecblic opened this issue · 5 comments

Hello,

I am experiencing an issue with controller.onDispose in the ultralytics_yolo package. Despite calling controller.dispose() in the dispose method of my widget, it seems that the onDispose callback does not execute as expected.

Here's the relevant part of my code:
@OverRide
void dispose() {
if (objectDetectorInstance != null) {
objectDetectorInstance.ultralyticsYoloPlatform.closeCamera();
objectDetectorInstance.ultralyticsYoloPlatform.pauseLivePrediction();
}

if (controller != null) {
controller.dispose();
}

_isControllerDisposed = true;

super.dispose();
}

void _disposeResources() {
if (!_isControllerDisposed) {
_isControllerDisposed = true;
controller.pauseLivePrediction();
controller.closeCamera();
objectDetectorInstance.ultralyticsYoloPlatform.closeCamera();
objectDetectorInstance.ultralyticsYoloPlatform.pauseLivePrediction();
}
}

The dispose method is supposed to clean up resources, but it appears that the controller.onDispose callback does not function as anticipated.

Has anyone encountered a similar issue or have suggestions on how to resolve this?

Thank you!

It sounds like you're dealing with an issue where the controller.onDispose callback is not being triggered as expected, even though you're explicitly calling controller.dispose() in your widget's dispose method.

Here are a few things you could check or try to resolve the issue:

1. Ensure the onDispose Callback is Properly Set

Make sure that the onDispose callback is actually being set on the controller. This could be as simple as a typo or forgetting to set it in the controller initialization.

controller = SomeController(
  onDispose: () {
    // Your dispose logic here
  },
);

2. Check if dispose() is Actually Being Called

Add a print statement or a breakpoint inside the dispose() method to confirm that it is indeed being called when you expect it to be. It's possible that the dispose() method is not being called due to the widget lifecycle not behaving as expected.

@override
void dispose() {
  print('dispose called'); // Debugging statement

  if (objectDetectorInstance != null) {
    objectDetectorInstance.ultralyticsYoloPlatform.closeCamera();
    objectDetectorInstance.ultralyticsYoloPlatform.pauseLivePrediction();
  }

  if (controller != null) {
    controller.dispose();
  }

  _isControllerDisposed = true;

  super.dispose();
}

3. Check for Any Async Operations

If there are any asynchronous operations inside dispose or elsewhere that might delay or prevent the dispose method from completing as expected, it could interfere with the execution of onDispose.

4. Sequence of Disposal

Ensure that the controller.dispose() is called at the correct point in the lifecycle, especially if there are other cleanup operations that might affect it. For instance, calling controller.dispose() after some other resource has been disposed of might cause it to fail silently.

@override
void dispose() {
  if (controller != null) {
    controller.dispose(); // Ensure this is called early if necessary
  }

  if (objectDetectorInstance != null) {
    objectDetectorInstance.ultralyticsYoloPlatform.closeCamera();
    objectDetectorInstance.ultralyticsYoloPlatform.pauseLivePrediction();
  }

  _isControllerDisposed = true;

  super.dispose();
}

5. Review the Implementation of dispose() in the Controller

There might be an issue within the dispose() method of the controller itself. If the onDispose callback is not correctly called within the dispose() implementation, that could be the root cause.

6. Force Call onDispose

As a temporary workaround, you could manually call the onDispose callback before or after calling controller.dispose() to ensure it gets executed.

if (controller != null) {
  controller.onDispose?.call(); // Manually trigger the callback
  controller.dispose();
}

If these steps don't resolve the issue, there might be something deeper going on in the ultralytics_yolo package or in how the controller is managed. Reviewing the implementation or adding more detailed logging might give more insight.

Hey, @glenn-jocher
It is not user issue, but the yolo-flutter-app issue. No onDispose method is defined in ultralytics_yolo_camera_controller.dart. And even if you try to manually call controller.closeCamera() or controller.pauseLivePrediction() then it has no effect because these methods are not implemented in Android:
https://github.com/ultralytics/yolo-flutter-app/blob/aa467274c3acba335ebd99772e3168cd54aaae67/android/src/main/java/com/ultralytics/ultralytics_yolo/MethodCallHandler.java#L264C1-L279C6

@timukasr thank you for pointing this out. Please ensure you're using the latest version of the package, as updates may address these issues. If the problem persists, consider opening a detailed issue on GitHub for further investigation by the team.

Latest version cannot work if we can see from code that the feature is not implemented. There is already two issues, the current one and #21, don't see how creating a third one would help.

@timukasr thank you for your feedback. We'll review the existing issues to address the missing implementations. Please stay tuned for updates.