Titanium JIRA Archive
Alloy (ALOY)

[ALOY-1361] Destroy calling off on variables that may be undefined

GitHub Issuen/a
Resolution Date2016-03-24T00:41:09.000+0000
Affected Version/sn/a
Fix Version/salloy 1.8.0
Labelsalloy, databinding, destroy
ReporterBrenton House


If you have an Alloy component that uses databinding and has conditional code defined in the xml, the destroy function is not taking into consideration the fact that the component may be undefined before calling the off function. Alloy xml:
	           <TableViewRow id="trainer_row" layout="composite" class="listitem_row" hasChild="true" onClick="$.togglePicker" if="$.args.editmode">
                        <View height="50" top="0" layout="composite" touchEnabled="false">
                            <Label text="Trainer" class="listitem_settings_name" touchEnabled="false" />
                            <Label text="{mymodel.trainer_name}" class="listitem_settings_value_haschild" touchEnabled="false" />
                        <Picker id="trainer" width="350" height="150" visible="false" onChange="$.onChange" top="40">
                            <Column dataCollection="trainer">
                                <Row title="{name}" value="{id}" />
Generated code:
  if ($.args.editmode) {

       // ------ only showing relevant code  ------

         var __alloyId287 = Alloy.Collections["trainer"] || trainer;
        __alloyId287.on("fetch destroy change add remove reset", __alloyId288);


    exports.destroy = function() {
        __alloyId281.off("fetch destroy change add remove reset", __alloyId282);
        __alloyId293.off("fetch destroy change add remove reset", __alloyId294);
        __alloyId314.off("fetch destroy change add remove reset", __alloyId315);
        __alloyId330.off("fetch destroy change add remove reset", __alloyId331);
	Alloy.Models.mymodel.off("fetch change destroy", __alloyId346);

In this case, __alloyId281 is actually undefined because it was declared and a value assigned inside the conditional if block. This is a pretty serious issue as I believe the error that occurs is stopping the other (existing) off functions from being called and causing a memory leak.


  1. Brenton House 2016-02-25

    Added PR: https://github.com/appcelerator/alloy/pull/762
  2. Sharif AbuDarda 2016-02-25

    This is a valid PR. Thanks for letting us know. Our engineer will look into it.
  3. Feon Sua Xin Miao 2016-03-24

    PR merged.

JSON Source