ncsms-android
ncsms-android copied to clipboard
Misuse about AsyncTask
According to our research, there are misuses about the following two AsyncTask classes:
- fr.unix_experience.owncloud_sms.engine.ASyncSMSRecovery.SMSRecoveryTask
- fr.unix_experience.owncloud_sms.engine.ASyncContactLoad.ContactLoadTask
The problems are:
- 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
- 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:
- I think the GUI-related fields should be wrapped into WeakReference. Take
private final RestoreMessagesActivity _context
as example, it can be changed tpprivate final WeakReference<RestoreMessagesActivity> _context
. - 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 invokecancel()
in the onDestroy() method of Activities.
These are my suggestions above, thank you.
Hello, feel free to propose a PR