I have code like this.
Class MyActivity {
private lateinit var downLoadQRHandler: DownloadQRHandler
...
fun someFun()
{ val model = getDataModelSomeOtherFun()
downLoadQRHandler = DownloadQRHandler(downloadQRModel, this)
}
}
As the activity is passed to one of the dependencies?
Will it cause Memory Leaks? At first I though, yes it would. BUt I think, after activity getting destroyed this reference downLoadQRHandler will automatically nullify. And MyActivity instance will be garbage collected.
to be on the safe side, I though of adding this in onDestroy()
override fun onDestroy() {
super.onDestroy()
downLoadQRHandler = null
}
Does it have any benefit of adding the ..= null code?
The activity class was flagged in the memoryProfiler and leakCanary. There could be other reasons for the memory leaks.
Without see other piece of code it's not possible to answer you. If "downLoadQRHandler" is used elsewhere and retained in memory, yes it could be a memory leak especially if DownloadQRHandler() constructor (which accepts an Activity) holds the Activity for later use.