detach event handlers has bug in code

The following code checks the length of the associative array incorrectly

function detach(eventName, className, handler, root) {
	if (eventHandlers[eventName]) {
		for(var i = 0; i < eventHandlers[eventName].length; i++){
			if(eventHandlers[eventName][i].root == root){
				eventHandlers[eventName].splice(i, 1);
				i--;
			}
		}
	}
}

The correct way to check the length is ‘Object.keys(eventHandlers[eventName]).length’ not ‘eventHandlers[eventName].length’

function detach(eventName, className, handler, root) {
	if (eventHandlers[eventName]) {
		for(var i = 0; i < Object.keys(eventHandlers[eventName]).length; i++){
			if(eventHandlers[eventName][i].root == root){
				eventHandlers[eventName].splice(i, 1);
				i--;
			}
		}
	}
}

This bug results in a buildup of event handlers each time the timeline is initialized after a destroy.

Here is what eventHandlers[‘click’] is holding after 5 reloads

click:
gantt_add:(5) [{…}, {…}, {…}, {…}, {…}]
gantt_close:(5) [{…}, {…}, {…}, {…}, {…}]
gantt_grid_head_cell:(5) [{…}, {…}, {…}, {…}, {…}]
gantt_header_arrow:(25) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
gantt_open:(5) [{…}, {…}, {…}, {…}, {…}]
gantt_row:(5) [{…}, {…}, {…}, {…}, {…}]
gantt_scale_cell:(5) [{…}, {…}, {…}, {…}, {…}]
gantt_task_link:(5) [{…}, {…}, {…}, {…}, {…}]

While trying to fix the code locally I realize the entire function is written for a indexed array so more code changes are required

I will post a solution shortly

Here is the correct implementation

		function detach(eventName, className, handler, root) {
			if (eventHandlers[eventName]) {
				var keys =  Object.keys(eventHandlers[eventName]);
				for(var i = 0; i < keys.length; i++){
					var key = keys[i];
					for(var j = 0; j < eventHandlers[eventName][key].length; j++){
						if(eventHandlers[eventName][key][j].root == root){
							eventHandlers[eventName][key].splice(j, 1);
							j--;
						}
					}
				}
			}
		}

Anyone from the DHTMLX team going to recognize this pretty huge bug?

Will it be fixed? If so when?

Hello,
thanks, we’ve confirmed the bug.
It is fixed in the current dev version and will be a part of the next update