Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-14393

Android: JS proxies are prematurely cleaned up when dalvik gc is triggered

    Details

    • Type: Bug
    • Status: Closed
    • Priority: High
    • Resolution: Fixed
    • Affects Version/s: Release 3.1.1
    • Fix Version/s: Release 4.1.0
    • Component/s: Android
    • Labels:
    • Story Points:
      34
    • Sprint:
      2015 Sprint 11 SDK

      Description

      Originally from https://jira.appcelerator.org/browse/TIMOB-13664:

      The problem is about Javascript scope for Titanium objects. In the logs, the problem was clearly after a garbage collect, so, first of all, we created a small module called "androidgc" which simply launch Android garbage collector (not the V8 GC).
      You can find this module here : https://github.com/ChrOnOs83/TiModuleAndroidGC

      Then, we decided to use your files and made it as simple as possible. We finally got a unique app.js file with this content :

      function createView(){
      	  var view = Ti.UI.createView({backgroundColor: 'white'});
        
      	  var gc = Ti.UI.createButton({top: '10dp',title: "First, click here many time quickly (cause GC)"});
      	  var btn = Ti.UI.createButton({title: "Next, click here"});
      	  var label = Ti.UI.createLabel({text: 'Hello World', bottom: '10dp'});
      	  
      	  view.add(btn);
      	  view.add(gc);
      	  
      	  btn.addEventListener('click', function(){
      		  view.add(label);
      	  });
      	  
      	  var androidgc = require('ti.nartex.androidgc');
      	  gc.addEventListener('click', function(){
      		androidgc.gc();
      	  });
      	  
      	  return view;
      }
       
       
      var win = Ti.UI.createWindow({});
      win.open();
       
      var view = createView();
      win.add(view);
      

      When you run this code on your Android Device, you should press many times on the "GC" button, because garbage collector may not clean your ressources each time...

      If GC has be done correctly, the second button press will fail, because the label is not here anymore.
      If you add the label to the view, and then change its title, no problem
      If you create the label in the event listener, no problem

      If you makes a reference to a global variable (without the var keyword for example), it works

      If you put the function in a CommonJS module, the same rules apply.

      So... Titanium objects created in a function (all the Ti.something) may be killed by garbage collector at any time if they are not attached to a view/window, or declared as global var.

      It breaks the Javascript scope pattern...

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                penrique Pedro Enrique (Inactive)
                Reporter:
                ayeung Allen Yeung
                Reviewer:
                Hieu Pham
              • Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Backbone Issue Sync

                  • Titanium SDK/CLI <> Titanium Mobile
                    Synced with:
                    TIMOB-16864
                    Sync status:
                    ERROR
                    Last received:
                    Last sent:

                    Git Source Code