Destructors icon indicating copy to clipboard operation
Destructors copied to clipboard

Readme.md is outdated; example DtorType.Function causes leak

Open caroparo opened this issue 3 years ago • 3 comments

Sample code uses dtor_track instead of dtor; anonymous function variable somehow causes the weak reference to stay alive ("Destructed!" is never printed).

caroparo avatar Nov 08 '22 08:11 caroparo

function foo1() constructor {
	dtor(DtorType.Function, function () {
		show_debug_message("Destructed 1!");
	});
}

var a1 = new foo1();
delete a1;
function foo2() constructor {
	func = function () {
		show_debug_message("Destructed 2!");
	};
	dtor(DtorType.Function, func);
}

var a2 = new foo2();
delete a2;
function foo3() constructor {
	function func() {
		show_debug_message("Destructed 3!");
	};
	dtor(DtorType.Function, func);
}

var a3 = new foo3();
delete a3;

Weirdly only a3 works.

caroparo avatar Nov 08 '22 09:11 caroparo

Hi thanks for reporting the error. try replacing the DtorInstance code with the following code

/// @ignore
/// @param {enum.DtorType} type
/// @param {Any} value
/// @param {Any} [options]
/// @param {Any} [reference]
function DtorInstance(_type, _value, _option, _ref) constructor {
	static use = {}; // Bound Methods to this struct
	
	reference = weak_ref_create(_ref);
	type  = _type;
	// Not bound
	value  = (is_method(_value) ) ? method(use, _value) : _value;
	option = _option;
}

Totobal5 avatar Nov 08 '22 12:11 Totobal5

Thanks for the fix!

caroparo avatar Nov 09 '22 02:11 caroparo