You are indeed causing a leak in a nasty way. What's going on is the following:
- The button is attached to a window
- You press the button
- The button executes the click listener
- In the click listener you setContentView()
- This in turns detaches the button from the window
- The button finishes handling the click and tries to unset its
pressed state by calling View.post(Runnable)
- There's no window, so the Runnable is enqueued in ViewRoot
- The runnable queue is never looked at because the first layout
already happened
To fix it's simple:
@Override
public void onClick(View v) {
v.post(new Runnable() {
public void run() {
setMode();
}
}):
}
I'll add a "fix" in Eclair for this but really you should consider
what you're doing. Changing the content view is not really how Android
apps usually work, instead you should use a different activity.
On Mon, Aug 17, 2009 at 6:26 PM, Dan Sherman<[email protected]> wrote:
> Dump is attached. Shows the application state after about 50 clicks on the
> button.
>
> Again, sorry for my ineptitude in the dump analysis, just haven't worked
> with them enough.
>
> Looks to be that mActions (ArrayList) is growing with lots of
> android.view.ViewRoot$
> RunQueue$HandlerAction (which I imagine is a callback that gets setup with
> the OnClickListener).
>
> The code used to create is as follows:
>
> public class SetContentTest extends Activity implements OnClickListener {
> @Override
> public void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setMode();
> }
>
> @Override
> public void onClick(View v) {
> setMode();
> }
>
> public void setMode() {
> setContentView(R.layout.main);
> ((Button) findViewById(R.id.btn_main)).setOnClickListener(this);
> }
> }
>
> Thanks again :)
>
>
> On Mon, Aug 17, 2009 at 9:21 PM, Dan Sherman <[email protected]> wrote:
>>
>> Dumps are attached. One pre-leak (start of application), and one after
>> about 50ish clicks on the button, which causes the leak.
>>
>> Again, sorry for my ineptitude in the dump analysis, just haven't worked
>> with them enough.
>>
>> Looks to be that mActions (ArrayList) is growing with lots of
>> android.view.ViewRoot$RunQueue$HandlerAction (which I imagine is a callback
>> that gets setup with the OnClickListener).
>>
>> The code used to create is as follows:
>>
>> public class SetContentTest extends Activity implements OnClickListener {
>> @Override
>> public void onCreate(Bundle savedInstanceState) {
>> super.onCreate(savedInstanceState);
>> setMode();
>> }
>>
>> @Override
>> public void onClick(View v) {
>> setMode();
>> }
>>
>> public void setMode() {
>> setContentView(R.layout.main);
>> ((Button) findViewById(R.id.btn_main)).setOnClickListener(this);
>> }
>> }
>>
>> Thanks guys :)
>>
>>
>> On Mon, Aug 17, 2009 at 2:11 PM, Romain Guy <[email protected]> wrote:
>>>
>>> Please do.
>>>
>>> On Mon, Aug 17, 2009 at 10:45 AM, Dan Sherman<[email protected]> wrote:
>>> > I'll check it out tonight when I get access to the dump again. But
>>> > from
>>> > what I remember, it was just a really long list of nested Runnables
>>> > (I'm
>>> > sure I missed something).
>>> >
>>> > Would it help if I posted the dump?
>>> >
>>> > - Dan
>>> >
>>> > On Mon, Aug 17, 2009 at 1:39 PM, Dianne Hackborn <[email protected]>
>>> > wrote:
>>> >>
>>> >> On Mon, Aug 17, 2009 at 9:57 AM, Dan Sherman <[email protected]>
>>> >> wrote:
>>> >>>
>>> >>> Looks to be a Runnable (from what I can tell).
>>> >>
>>> >> It can't just be a Runnable, it needs to be a concrete implementation
>>> >> of
>>> >> Runnable. I am pretty sure hat will show you the actual class
>>> >> implementation, since it only deals in concrete classes. This may be
>>> >> an
>>> >> (anonymous) inner class, in which case there should be fields in it
>>> >> you can
>>> >> follow to get back to a more interesting class.
>>> >>
>>> >> --
>>> >> Dianne Hackborn
>>> >> Android framework engineer
>>> >> [email protected]
>>> >>
>>> >> Note: please don't send private questions to me, as I don't have time
>>> >> to
>>> >> provide private support, and so won't reply to such e-mails. All such
>>> >> questions should be posted on public forums, where I and others can
>>> >> see and
>>> >> answer them.
>>> >>
>>> >>
>>> >>
>>> >
>>> >
>>> > >
>>> >
>>>
>>>
>>>
>>> --
>>> Romain Guy
>>> Android framework engineer
>>> [email protected]
>>>
>>> Note: please don't send private questions to me, as I don't have time
>>> to provide private support. All such questions should be posted on
>>> public forums, where I and others can see and answer them
>>>
>>>
>>
>
>
> >
>
--
Romain Guy
Android framework engineer
[email protected]
Note: please don't send private questions to me, as I don't have time
to provide private support. All such questions should be posted on
public forums, where I and others can see and answer them
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---