ultralytics / yolo-flutter-app

A Flutter plugin for Ultralytics YOLO computer vision models
https://ultralytics.com
GNU Affero General Public License v3.0
61 stars 24 forks source link

controller.onDispose Not Working Properly #50

Open nikhilgarala-tecblic opened 1 month ago

nikhilgarala-tecblic commented 1 month ago

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!

glenn-jocher commented 1 month ago

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.