bobo2000
bobo2000

Reputation: 1877

Remove event handler from memory (ortc) , titanium

I have a memory leak:

    var ortc = require("co.realtime.ortc");

        function ortcNot() {

                ortc.clusterUrl = 'http://ortc-developers.realtime.co/server/2.2';
                ortc.connectionMetadata = 'Titanium Example';

                ortc.addEventListener('onException', function(e) {
                    Ti.API.info('Exception: ' + e.info);
                });

                ortc.addEventListener('onConnected', function(e) {
                    Ti.API.info('Connected to ORTC server');
                    ortc.subscribe('yellow24', true);
                });

                ortc.addEventListener('onDisconnected', function(e) {
                    Ti.API.info('Disconnected from ORTC');
                    //remove event handlers
                    //ortc = null;

                });

                ortc.addEventListener('onSubscribed', function(e) {
                    Ti.API.info('Subscribed to: ' + e.channel);
                    Ti.API.info('Sending a message to: ' + e.channel);
                    //ortc.send(e.channel, 'Message from iPhone');
                });

                ortc.addEventListener('onUnsubscribed', function(e) {
                    Ti.API.info('Unsubscribed from: ' + e.channel);
                    ortc.disconnect();
                });

                ortc.addEventListener('onMessage', function(e) {
                    Ti.API.info('Message received: ' + e.message + ' at channel: ' + e.channel);
                    //parse message
                    var message = JSON.parse(e.message);

                    alert(message.user.message);

                    //check chat id
                    if (message.id == args.chatId) {

                        recieveMessage(message);

                    }
                    //ortc.unsubscribe(e.channel);
                    Ti.API.info(ortc.isConnected());
                });


            ortc.connect('yellow2');

        }

ortcNot();

When I close my controller window, then reopen the window. The old event listeners are still in memory, causing duplicate event listeners to be created.

Any idea why this is happening,and how to solve it? Thanks

Upvotes: 1

Views: 201

Answers (1)

Michael
Michael

Reputation: 276

Instead of defining listeners with an in-built function, always declare them to a calling function.

To remove an eventListener it has to be identical in both addEventListener and removeEventListener, declaring it as a function and assigning this to the eventListener gets around this. e.g.

var viewObject = null;

function eventTodo(){
  // do stuff here ...

  viewObject.removeEventListener('click', eventTodo);
}

function addListener() {
    viewObject = Ti.UI.createView();
    viewObject.addEventListener('click', eventTodo);

   return viewObject;
}

When you close the window you are not calling the removeEventLister which is why every time the window is opened the events are being duplicated. Attach an close event to the window object in question that calls a function to remove all the eventListeners thus removing them from the applications memory.

Upvotes: 1

Related Questions