This fix checks to make sure the stopPolling and closeConnection methods actually exist before calling them. If the device is a placeholder (i.e. was never actually connected), and an attempt is made to call these non-existent methods, then it will cause an error. The error will be caught by node-red but will cause the entire node.on('close') event callback to cease and therefore any other devices will not be disconnected. Those still-connected devices will continue to poll, resulting in a memory leak. By checking to make sure these methods exist, it will prevent an error from occurring, thereby prevent a memory leak.
This fix checks to make sure the
stopPolling
andcloseConnection
methods actually exist before calling them. If the device is a placeholder (i.e. was never actually connected), and an attempt is made to call these non-existent methods, then it will cause an error. The error will be caught by node-red but will cause the entirenode.on('close')
event callback to cease and therefore any other devices will not be disconnected. Those still-connected devices will continue to poll, resulting in a memory leak. By checking to make sure these methods exist, it will prevent an error from occurring, thereby prevent a memory leak.