gjunkie
gjunkie

Reputation: 828

setTimeout memory leak in Titanium Appcelerator

I've done a little bit of research and have found that using setTimeout() causes a bad memory leak, as seen on this post. I'm hoping to find a remedy or an alternative.

What I have is a small view appearing on the screen when any of my many buttons is touched. At the same time I set a timeout to fadeout the small view after 3 seconds. When the button is first pressed, I also clear the timeout so that I don't continue setting multiple ones. Though now while analyzing my code I see that I'm setting an interval and clearing a timeout. Not sure if this is part of my issue. It looks like this:

var Modal = Titanium.UI.createView({
    width:151,
    height:83,
    owner: null,
    myView: null,
});

var modalTimer;
var addModal = function(){
    clearInterval(modalTimer);
    theView.add(Modal);
    modalTimer = setTimeout( function() {
        removeModal();
        changeTurn();
},3000);
}

playerButton.addEventListener('click',function(){
    addModal(); 
});

Thanks!!!

Upvotes: 1

Views: 3458

Answers (2)

David Price
David Price

Reputation: 151

I don't know if this helps but I have noticed that my app crashes if i use:

modalTimer = setTimeout(removeModal(),3000);

but doesn't if i use

modalTimer = setTimeout(removeModal, 3000);

where removeModal is defined as

var removeModal = function()
{...};

Upvotes: 0

gjunkie
gjunkie

Reputation: 828

I may have solved this issue with the following:

var Modal = Titanium.UI.createView({
    width:151,
    height:83,
    owner: null,
    myView: null,
});

var modalTimer;
var addModal = function(){
    clearTimeout(modalTimer);
    theView.add(Modal);
    modalTimer = setTimeout(removeModal(),3000);
}

playerButton.addEventListener('click',function(){
    addModal(); 
});

I put my changeTurn() function in my removeModal() function, and removed the anonymous function from setTimeout. I also corrected the clearTimeout confusion. I still have to see how well this holds up over extended gameplay, but from first impressions this has fixed my issues. I hope this helps anyone with similar issues.

Upvotes: 1

Related Questions