Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-2969] Android: event listeners orphaned from their contexts continue to be invoked

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionWon't Fix
Resolution Date2017-07-28T19:30:01.000+0000
Affected Version/sRelease 3.0.0
Fix Version/sn/a
ComponentsAndroid
Labelsapi
ReporterBill Dawson
AssigneeIngo Muschenetz
Created2011-04-15T03:33:54.000+0000
Updated2017-07-28T19:30:01.000+0000

Description

This is most notable if the event listener is added to a module as opposed to another type of proxy, because modules are effectively singletons (created once in the root context) and therefore their event listener collections survive long.

As a sample failcase, create a new Ti project and make this the app.js:

Titanium.UI.setBackgroundColor('#000');
var win = Titanium.UI.createWindow({  
    title:'Test',
    backgroundColor:'#fff',
    exitOnClose: true
});

var btn = Ti.UI.createButton({
    title: 'Open second window'
});
btn.addEventListener('click', function() {
    Ti.UI.createWindow({
        url: 'win.js',
        backgroundColor: 'black',
        fullscreen: false
    }).open();
});
win.add(btn);
win.open();

Then create a new file, win.js, with this in it:

var win = Ti.UI.currentWindow;
Ti.App.addEventListener('test_event', function(){Ti.API.info("TEST EVENT LISTENER");});
var btn = Ti.UI.createButton({
    top: 10, left: 30, right: 30, height: 40,
    title:'Fire event'
});
btn.addEventListener('click',function(){
    Ti.App.fireEvent('test_event', {});
});
win.add(btn);

var btnClose = Ti.UI.createButton({
    top: 60, left: 30, right: 30, height: 40,
    title: 'Close window'
});
btnClose.addEventListener('click', function(){win.close();});
win.add(btnClose);

Run the application and do the following:

  • Click "Open second window."
  • At the next window, click the "Fire event" button.
  • Look at the console: you'll see one info message "TEST EVENT LISTENER".
  • Now back out of this window to go back to the first window.
  • Again click "Open second window."
  • Again click "Fire event".
  • Look at the console: two more "TEST EVENT LISTENER" info messages will have appeared, when we would expect only one more.

If you keep going back and forth between the windows like this, you'll keep getting more and more "TEST EVENT LISTENER" info messages -- these are event listeners stranded from all the other times that the second window was opened.

It could be argued that our user-devs are responsible for removing event listeners when they are no longer wanted, such as when a heavyweight window closes and that heavyweight window's JS code created the event listeners. However, this problem does not exist on iPhone.

Comments

  1. Bill Dawson 2011-04-15

    Assigning to Don for milestoning

  2. Junaid Younus 2012-08-29

    Tested on a Samsung Galaxy S2 using TiSDK 2.2.0v20120828153312, issue still valid.
  3. Lee Morris 2017-07-28

    Closing due to inactivity. If this issue still exists, please raise a new ticket.

JSON Source