我有内存泄漏:
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();
当我关闭我的控制器窗口时,然后重新打开该窗口。旧的事件侦听器仍在内存中,导致创建重复的事件侦听器。
知道为什么会发生这种情况,以及如何解决吗?谢谢