c# my destructor isn't being called?

前端 未结 3 1369
说谎
说谎 2021-01-14 11:54

I have this simple code and trying to call the destructor but I can\'t call it :(

I know that GarbageCollector runs when it\'s necessary, so I used GC.WaitForPending

3条回答
  •  一个人的身影
    2021-01-14 12:18

    If you run a program with the debugger attached it changes the behavior of the lifetime of objects.

    Without the debugger a object becomes ellagable for collection as soon as the last use of the object has been passed in the code. With the debugger attached the lifetime of all objects get extended to the entire time the object is in scope, this is done so you can view the object in the Watch window of the debugger and not have the object collected out from under you.

    You must either run your program in release mode without the debugger attached or set calculator to null before you call GC.Collect() to be able to have the object be eligible for garbage collection and have it's finalizer run.

提交回复
热议问题