ncsms-android icon indicating copy to clipboard operation
ncsms-android copied to clipboard

Misuse about AsyncTask

Open pangeneral opened this issue 5 years ago • 1 comments

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<RestoreMessagesActivity> _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.

pangeneral avatar Aug 13 '19 06:08 pangeneral

Hello, feel free to propose a PR

nerzhul avatar Sep 23 '19 18:09 nerzhul