coil-kt / coil

Image loading for Android and Compose Multiplatform.
https://coil-kt.github.io/coil/
Apache License 2.0
10.76k stars 660 forks source link

[Feature Requsst] Add a `close` or `dispose` method to the `Decoder` interface for resource cleanup #2624

Open bc-lee opened 13 hours ago

bc-lee commented 13 hours ago

Is your feature request related to a problem? Please describe. Currently, the Decoder interface in coil lacks a standardized way to handle resource cleanup. When developers implement custom Decoder classes that rely on native libraries (e.g., libavif for AVIF decoding), they often need to release resources explicitly when decoding is complete. However, without a close or dispose method, there’s no consistent mechanism to safely handle resource cleanup, which can lead to resource leaks or necessitate complex workarounds.

Describe the solution you'd like Adding a close or dispose method to the Decoder interface would enable custom Decoder implementations to reliably release resources when they’re no longer needed. This method would give developers an easy, built-in way to handle cleanup, allowing coil to automatically call close on the Decoder instance when decoding is finished. This solution would align with the Dispose pattern, common in garbage-collected languages, and enhance coil's flexibility for those using external or native resources in custom decoders.

To ensure minimal impact on existing Decoder implementations, a default (empty) implementation of close could be provided, requiring no changes to existing decoders that don’t need explicit cleanup.

Why this solution makes sense for coil
This proposal aligns well with coil's goals for the following reasons:

  1. Increased Flexibility and Safety: The close or dispose method would allow custom decoders to handle resource management consistently, supporting a broader range of use cases without risk of resource leaks. This change would be particularly beneficial for users working with resource-intensive or native libraries.

  2. Minimal Impact on Core Users: By providing a default implementation for close, this proposal ensures that typical coil users and existing Decoder implementations remain unaffected, keeping the API simple and intuitive.

  3. Standardized Cleanup Mechanism: A close method would create a consistent, reliable way to handle resource release, avoiding the need for developers to implement complex external workarounds like wrappers or use blocks. This aligns with Kotlin’s emphasis on developer ergonomics and ease of use.

  4. Alignment with Established Patterns: The Dispose pattern is a widely recognized solution for resource management, especially in garbage-collected languages. Adding close to Decoder would bring coil in line with best practices for handling external resources.

Additional context
While this proposal introduces a slight increase in API complexity, it is optional for most use cases and provides clear benefits to those needing resource management. For further context on the Dispose pattern, see Dispose Pattern in Wikipedia. Adding this method makes coil more versatile and adaptable for custom decoder use cases, enhancing its value as a flexible, developer-friendly library.

colinrtwhite commented 12 hours ago

The current intention is that all decoding work is performed inside the Decoder.decode block exactly once. Any resources can be cleaned up when returning from that function as the ImageSource is exhausted and decoding is complete. Does that cover your use case?