This repository was archived by the owner on Jun 1, 2024. It is now read-only.
This repository was archived by the owner on Jun 1, 2024. It is now read-only.
Misuse of AsyncTask #3051
Open
Description
According to our research, there are misuses about the following classes:
me.ccrama.redditslide.Vote
me.ccrama.redditslide.Activities.CommentsScreenSingle.AsyncGetSubredditName
me.ccrama.redditslide.Activities.Inbox
me.ccrama.redditslide.Activities.LiveThread
The problems are:
- There are many annoymous inner AsyncTask classes in
LiveThread
andInbox
. 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. Similar problem also exists inVote
andAsyncGetSubredditName
. - The instances of these AsyncTask classes 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 View v
inVote
as example, it can be changed tpprivate WeakReference<view> v
. Besides, use non-anonymous inner static classes to replace these annoymous classes. - Add a AsyncTask field in the corresponding Activities which use AsyncTask. These field refer to the annoymous AsyncTask object. Then invoke
cancel()
in the onDestroy() method of Activities.
These are my suggestions above, thank you.