Project: titanium
Issue TIMOB-14393: Android: JS proxies are prematurely cleaned up when dalvik gc is triggered 
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 : {code} 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); {code} 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...
Story points: 34