C# Destructor not calling after out of scope -
i have problem destructor after going out of scope(it calling after time , need make action on form, example change radio button), maybe there's mistake in code. take look:
namespace windowsformsapplication2 { public partial class form1 : form { public form1() { initializecomponent(); eventlogger.print += delegate(string output) { if (!textbox1.isdisposed) this.invoke(new methodinvoker(() => textbox1.appendtext(output + environment.newline)), null); }; } private void button1_click(object sender, eventargs e) { testclass test = new testclass(); } } public static class eventlogger { public delegate void eventhandler(string output); public static event eventhandler print; public static void addlog(string textevent) { print(textevent); } } public class testclass { public testclass() { eventlogger.addlog("testclass()"); } ~testclass() { eventlogger.addlog("~testclass()"); } } }
right, because isn't c++. finalizer (not destructor in c++) not guaranteed called after object has left declaring scope, called when gc decides swoop in , clean after you.
may ask why using finalizer begin with? maintaining references unmanaged resources need deallocated deterministically possible (if so, read on idisposable interface)? use cases c# finalizers few , far between, it's not common implement them.
Comments
Post a Comment