reddinator icon indicating copy to clipboard operation
reddinator copied to clipboard

StrongReference of AsyncTask

Open pangeneral opened this issue 6 years ago • 3 comments
trafficstars

au.com.wallaceit.reddinator.activity.ViewAllSubredditsActivity.DLTask is an inner class of au.com.wallaceit.reddinator.activity.ViewAllSubredditsActivity, which means The AsyncTask class holds strong reference to the Activity. In other words, there is a potential memory leak danger. I think DLTask should be changed to a static class or outter class. Thank you.

pangeneral avatar Aug 13 '19 02:08 pangeneral

I may be wrong but since the class is initialized into an object it doesn't matter that it's an inner class. Just like before, once the async task ends then it's object will be disposed. If the activity is disposed before that it is also fine.

Are you new to Java by any chance? You sound like a C programmer :-D

If you are interesting in contributing to the project I can send you a few stack traces from the Play console. I haven't had much time to work on this project in the last year or so. It could use some love.

micwallace avatar Aug 14 '19 10:08 micwallace

Maybe you misunderstood what I mean. The cancel operation of AsyncTask doesn't really destroy the background thread. It just change the status of AsyncTask to cancelled. And if the status of AsyncTask is cancelled, then onCancelled instead of onPostExecute will be invoked. If you didn't cancel it, then there might be problems if onPostExecute is invoked to update the UI while Activity has been destroyed. Of course, maybe you still want to invoke onPostExecute even the Activity is destroyed. If it is that case, then there won't be problem if you don't cancel AsyncTask.

By the way, I'm not new to Java. In fact, I have written Java since I was a postgraduate student.

------------------ 原始邮件 ------------------ 发件人: "micwallace"[email protected]; 发送时间: 2019年8月14日(星期三) 晚上6:36 收件人: "micwallace/reddinator"[email protected]; 抄送: "潘临杰"[email protected];"Author"[email protected]; 主题: Re: [micwallace/reddinator] StrongReference of AsyncTask (#25)

I may be wrong but since the class is initialized into an object it doesn't matter that it's an inner class. Just like before, once the async task ends then it's object will be disposed. If the activity is disposed before that it is also fine.

Are you new to Java by any chance? You sound like a C programmer :-D

If you are interesting in contributing to the project I can send you a few stack traces from the Play console. I haven't had much time to work on this project in the last year or so. It could use some love.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

pangeneral avatar Aug 14 '19 12:08 pangeneral

I think I intend to run post execute in most cases, although I haven't had a look at the code in a while. There should definitely be a null check on any activity context references. If you think it could be improved please feel free to submit a pull request.

On 14 August 2019 10:22:37 pm AEST, Linjie Pan [email protected] wrote:

Maybe you misunderstood what I mean. The cancel operation of AsyncTask doesn't really destroy the background thread. It just change the status of AsyncTask to cancelled. And if the status of AsyncTask is cancelled, then onCancelled instead of onPostExecute will be invoked. If you didn't cancel it, then there might be problems if onPostExecute is invoked to update the UI while Activity has been destroyed. Of course, maybe you still want to invoke onPostExecute even the Activity is destroyed. If it is that case, then there won't be problem if you don't cancel AsyncTask.>

By the way, I'm not new to Java. In fact, I have written Java since I was a postgraduate student.>

------------------ 原始邮件 ------------------> 发件人: "micwallace"[email protected];> 发送时间: 2019年8月14日(星期三) 晚上6:36> 收件人: "micwallace/reddinator"[email protected];> 抄送: "潘临杰"[email protected];"Author"[email protected];> 主题: Re: [micwallace/reddinator] StrongReference of AsyncTask (#25)>

I may be wrong but since the class is initialized into an object it doesn't matter that it's an inner class. Just like before, once the async task ends then it's object will be disposed. If the activity is disposed before that it is also fine.>

Are you new to Java by any chance? You sound like a C programmer :-D>

If you are interesting in contributing to the project I can send you a few stack traces from the Play console. I haven't had much time to work on this project in the last year or so. It could use some love.>

—> You are receiving this because you authored the thread.> Reply to this email directly, view it on GitHub, or mute the thread.>

-- > You are receiving this because you commented.> Reply to this email directly or view it on GitHub:> https://github.com/micwallace/reddinator/issues/25#issuecomment-521223603

-- Sent from my Android device with K-9 Mail. Please excuse my brevity.

micwallace avatar Aug 14 '19 22:08 micwallace