Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-7876] Android: Ti.App.addEventListener() from a commonJS unit causes later Ti.App.removeEventListener() to crash

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2012-03-14T11:54:33.000+0000
Affected Version/sn/a
Fix Version/sRelease 2.0.0, Sprint 2012-06
ComponentsAndroid
Labelscrash, module_view, qe-testadded, regression, release-note-2.0.0
ReporterShawn Lipscomb
AssigneePing Wang
Created2012-03-01T08:42:02.000+0000
Updated2012-07-05T12:03:26.000+0000

Description

Problem

When Ti.App.addEventListener() is called from a commonJS unit, other attempts to add an event listener to Ti.App on the same event in normal app units, and then remove that event listener, causes a crash: {noformat}----- Titanium Javascript Runtime Error ----- E/TiJSError( 667): (main) [1,3676] - In ti:/events.js:241,23 E/TiJSError( 667): (main) [0,3676] - Message: Uncaught TypeError: Cannot read property '0' of undefined E/TiJSError( 667): (main) [0,3676] - Source: if (list.listener[i] === listener || E/V8Exception( 667): Exception occurred at ti:/events.js:241: Uncaught TypeError: Cannot read property '0' of undefined{noformat} *This is a regression*, as it did not occur in SDK 1.8.1.

Testcase

Ti.App.addEventListener(a,b) is called from a commonJS unit. Another Ti.App.addEventListener(a,c) is called from app.js (notice that the first parameter, the event name, is the same in both calls). Also in app.js, Ti.App.removeEventListener(a,c) is called, which causes the crash. Put the following code into a new Ti mobile project and run it to the emulator. The crash occurs without any interaction, as soon as the main window appears.
var Procs=require('CommonProcs');
Procs.FirstStep();

var win1=Ti.UI.createWindow({
                    exitOnClose:true,
                    navBarHidden:true
                   })

var TheLabel=Ti.UI.createLabel({
        color:'white',
        text:'Hello World',
        width:'auto'
     });
win1.add(TheLabel);
win1.open();


function MainWinOrientationChange()
{
  Ti.API.info('MainWinOrientationChange');
}
Ti.App.addEventListener('CustomOrientationChange',MainWinOrientationChange);
Ti.App.removeEventListener('CustomOrientationChange',MainWinOrientationChange);
exports.FirstStep=function()
{
  function EmbededHandler()
  {
    Ti.API.info('test');
  }

  // Removing the following line or changing the first parameter
  // causes the crash to not occur.
  Ti.App.addEventListener('CustomOrientationChange',EmbededHandler);
};

Comments

  1. Ivan Skugor 2012-03-02

    I can confirm this issue for component-based events. I didn't try to isolate the issue, but I not using app-level events and I'm getting this error message when I compile app with 1.8.2 (1.8.1 don't have this issue).
  2. Arnaud Besnier 2012-03-07

    I confirm that I have the same issue with SDK 1.8.2 : when a window (with an event listener associated to it) is closed I have the same Javascript Runtime Error. The event listeners manager may not be able to find and delete the event listener associated to the window closed... I also confirm that the bug is not present in SDK 1.8.1.
  3. Shawn Lipscomb 2012-03-07

    Has this fix been merged into the 1_8_X branch?
  4. Dustin Hyde 2012-03-16

    Closing as Fixed. Tested with Ping's code. Expected Behavior: Removing event listeners will not cause the associated runtime error. SDK: 2.0.0.v20120316130302 Android: V8, Rhino Studio: 2.0.0.201203152033 OS: Snow Leopard Devices Tested: Nexus One 2.2.2 Note: This bug does not involve CommonJS Modules.
  5. Shawn Lipscomb 2012-03-19

    Verified fix on SDK 2.0.0.v20120319003254. Thanks!

JSON Source