{"action":"created","issue":{"url":"https://api.github.com/repos/sandeepmistry/node-sensortag/issues/57","labels_url":"https://api.github.com/repos/sandeepmistry/node-sensortag/issues/57/labels{/name}","comments_url":"https://api.github.com/repos/sandeepmistry/node-sensortag/issues/57/comments","events_url":"https://api.github.com/repos/sandeepmistry/node-sensortag/issues/57/events","html_url":"https://github.com/sandeepmistry/node-sensortag/issues/57","id":105647572,"number":57,"title":"EventEmitter leaks when SCAN_DUPLICATES = true","user":{"login":"cristian-t","id":8492948,"avatar_url":"https://avatars.githubusercontent.com/u/8492948?v=3","gravatar_id":"","url":"https://api.github.com/users/cristian-t","html_url":"https://github.com/cristian-t","followers_url":"https://api.github.com/users/cristian-t/followers","following_url":"https://api.github.com/users/cristian-t/following{/other_user}","gists_url":"https://api.github.com/users/cristian-t/gists{/gist_id}","starred_url":"https://api.github.com/users/cristian-t/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cristian-t/subscriptions","organizations_url":"https://api.github.com/users/cristian-t/orgs","repos_url":"https://api.github.com/users/cristian-t/repos","events_url":"https://api.github.com/users/cristian-t/events{/privacy}","received_events_url":"https://api.github.com/users/cristian-t/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-09-09T17:40:03Z","updated_at":"2015-09-15T17:17:07Z","closed_at":null,"body":"Ever since I switched to Node.js 0.12 I've been getting EventEmitter memory leak warnings. It seems when SCAN_DUPLICATES is set to true, events will be added for every \"discovery\" of the same sensor tag.\r\nI've posted this issue here since I am using ti sensor tags, but I assume this would happen with any BLE device."},"comment":{"url":"https://api.github.com/repos/sandeepmistry/node-sensortag/issues/comments/140470142","html_url":"https://github.com/sandeepmistry/node-sensortag/issues/57#issuecomment-140470142","issue_url":"https://api.github.com/repos/sandeepmistry/node-sensortag/issues/57","id":140470142,"user":{"login":"cristian-t","id":8492948,"avatar_url":"https://avatars.githubusercontent.com/u/8492948?v=3","gravatar_id":"","url":"https://api.github.com/users/cristian-t","html_url":"https://github.com/cristian-t","followers_url":"https://api.github.com/users/cristian-t/followers","following_url":"https://api.github.com/users/cristian-t/following{/other_user}","gists_url":"https://api.github.com/users/cristian-t/gists{/gist_id}","starred_url":"https://api.github.com/users/cristian-t/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cristian-t/subscriptions","organizations_url":"https://api.github.com/users/cristian-t/orgs","repos_url":"https://api.github.com/users/cristian-t/repos","events_url":"https://api.github.com/users/cristian-t/events{/privacy}","received_events_url":"https://api.github.com/users/cristian-t/received_events","type":"User","site_admin":false},"created_at":"2015-09-15T17:17:07Z","updated_at":"2015-09-15T17:17:07Z","body":"I've tracked down the issue and believe I fixed it as well.\r\nThe issue is in `noble-device > Util.inherits > constructor.onDiscover`\r\nA new `constructor` is created everytime `noble` emits a discover event, which happens very often for the same device when SCAN_DUPLICATES is true. The new `constructor` instance then creates a new `NobleDevice` instance which attaches a disconnect event to the same peripheral over and over again.\r\n\r\nThis is the temporary fix I applied:\r\n```\r\nconstructor.deviceList = {};\r\n\r\nconstructor.onDiscover = function(peripheral) {\r\n  if (constructor.is(peripheral)) {\r\n    var device;\r\n\r\n    if( constructor.deviceList.hasOwnProperty( peripheral.id ) )\r\n    {\r\n      device = constructor.deviceList[ peripheral.id ];\r\n    }\r\n    else\r\n    {\r\n      device = new constructor(peripheral);\r\n      constructor.deviceList[ peripheral.id ] = device;\r\n    }\r\n\r\n    constructor.emitter.emit('discover', device);\r\n  }\r\n};\r\n```\r\n\r\nI have `noble-device` force each `constructor` to maintain a `deviceList` and avoid creating a new `constructor` instance if it already exists. `deviceList` is also re-created every time `constructor.startScanning` is called.\r\n```\r\nconstructor.startScanning = function() {\r\n  constructor.deviceList = {};\r\n  noble.startScanning(constructor.SCAN_UUIDS, constructor.SCAN_DUPLICATES);\r\n};\r\n```"}}