stephanenicolas / toothpick

A scope tree based Dependency Injection (DI) library for Java / Kotlin / Android.
Apache License 2.0
1.12k stars 115 forks source link

Trying to connect LeakCanary with KTP #418

Open afaucogney opened 4 years ago

afaucogney commented 4 years ago

LeakCanary detects leaks which belong to official Android/Jetpack lifecycles.

With KTP, we may define our own instances management system with a scope-tree. When we open and close scopes, a lifecycle is then derived for every instance of the app. Nevertheless, dev can add code that trigger leaks, and even if the scope is closed and the instance is not injectable anymore, the instance is still there because of a leak.

LeakCanary supports custom lifecycle watcher, where we can tell it to watch if an instance will be soon garbage collected with : AppWatcher.objectWatcher.watch(viewAdapter, "the view adapter should be destroyed")

Is there a way to retrieve all instances that belong to a scope at runtime ? To enable an automatic verification of the leak less instances ? that could be at the end looking like this ?

KTP.openScope(this).getScopedInstances().forEach {
    AppWatcher.objectWatcher.watch(it, "the instance ${it.instance} of the class ${it.class.name} should be destroyed")`
}