nerzhul / ncsms-android

repository for the ncsms on Android
95 stars 38 forks source link

Misuse about AsyncTask #214

Open pangeneral opened 5 years ago

pangeneral commented 5 years ago
According to our research, there are misuses about the following two AsyncTask classes: 1. fr.unix_experience.owncloud_sms.engine.ASyncSMSRecovery.SMSRecoveryTask 2. fr.unix_experience.owncloud_sms.engine.ASyncContactLoad.ContactLoadTask The problems are: 1. They hold strong reference to the GUI element of Activity, which can lead to memory leak when the Activity is destroyed and the AsyncTask did not finish 2. Their instances are not cancelled before the Activity is destroyed, which can lead to the wrong invocation of onPostExecute I think we can make following changes to fix the misuse problems: 1. I think the GUI-related fields should be wrapped into WeakReference. Take `private final RestoreMessagesActivity _context` as example, it can be changed tp `private final WeakReference _context`. 2. Add a AsyncTask field in the corresponding Activities which use AsyncTask. These field refer to the annoymous AsyncTask object such as `new ASyncSMSRecovery.SMSRecoveryTask(me, _account).execute()`. Then invoke `cancel()` in the onDestroy() method of Activities. These are my suggestions above, thank you.
nerzhul commented 5 years ago

Hello, feel free to propose a PR