finalizer

C# WeakReference object is NULL in finalizer although still strongly referenced

时间秒杀一切 提交于 2019-11-29 05:11:06
Hi I have code here where I don't understand why I hit the breakpoint (see comment). Is this a Microsoft bug of something I don't know or I don't understand properly ? The code was tested in Debug but I think it should not changes anything. Note: You can test the code directly in a console app. JUST FOR INFORMATION... following supercat answer, I fixed my code with proposed solution and it works nicely :-) !!! The bad thing is the usage of a static dict and the performance the goes with it but it works. ... After few minutes, I realized that SuperCat give me all hints to do it better, to

Proper cleanup of WPF user controls

╄→尐↘猪︶ㄣ 提交于 2019-11-29 02:39:07
问题 I am relatively new to WPF, and some things with it are quite foreign to me. For one, unlike Windows Forms, the WPF control hierarchy does not support IDisposable. In Windows Forms, if a user control used any managed resources, it was very easy to clean up the resources by overriding the Dispose method that every control implemented. In WPF, the story is not that simple. I have searched for this for several hours, and encountered two basic themes: The first theme is Microsoft clearly stating

Will SqlConnection get disposed by GC?

坚强是说给别人听的谎言 提交于 2019-11-28 23:33:16
Disclaimer: I know IDisposable should be implemented when dealing with unmanaged resources. The rest of the code should be deterministic and do using (...) { } (equivalent of try {} finally { Dispose(); } ) to guarantee a cleanup as soon as possible. Also, the GC will not call Dispose() , so the recommended pattern is to override the Finalize() method (in C# using the destructor syntax) which then calls Dispose() . The GC will usually call Finalize() (unless GC.SuppressFinalize() has been called). Problem: So now that I got that out of the way, I have an odd scenario where I cannot do using

Should Java 9 Cleaner be preferred to finalization?

我们两清 提交于 2019-11-28 23:26:11
In Java, overriding the finalize method gets a bad rap, although I don't understand why. Classes like FileInputStream use it to ensure close gets called, in both Java 8 and Java 10. Nevertheless, Java 9 introduced java.lang.ref.Cleaner which uses the PhantomReference mechanism instead of GC finalization. At first, I thought it was just a way add finalization to third-party classes. However, the example given in its javadoc shows a use-case that can easily be rewritten with a finalizer. Should I be rewriting all of my finalize methods in terms of Cleaner? (I don't have many, of course. Just

Can a simple difference in Python3 variable names alter the way code runs? [duplicate]

人盡茶涼 提交于 2019-11-28 21:01:11
This question already has an answer here: Python attributeError on __del__ 1 answer This code... class Person: num_of_people = 0 def __init__(self, name): self.name = name Person.num_of_people += 1 def __del__(self): Person.num_of_people -= 1 def __str__(self): return 'Hello, my name is ' + self.name cb = Person('Corey') kb = Person('Katie') v = Person('Val') Produces the following error... Exception AttributeError: "'NoneType' object has no attribute 'num_of_people'" in <bound method Person.__del__ of <__main__.Person object at 0x7f5593632590>> ignored But this code does not. class Person:

How is an object marked as finalized in Java (so that the finalize method wouldn't be called the second time)?

最后都变了- 提交于 2019-11-28 20:56:35
The main question is in the topic but let me show my vision of finalization proccess in Java so that I can ask you a little more. Well the gc starts garbage collection by marking all live objects. When all reachable objects are marked as "live". All other objects are unreachable. The next step is to check every unreachable object and determine whether it can be sweeped right now or it should be finalized at first. The gc thinks the next way if the object's finalize method has a body then this object is finalizable and should be finalized; if the object's finalize method has an empty body

What if a finalizer makes an object reachable?

末鹿安然 提交于 2019-11-28 07:54:49
问题 In Java, finalize is called on an object (that overrides it) when it's about to be garbage collectioned, so when it's unreachable. But what if the finalizer makes the object reachable again, what happens then? 回答1: The object will not be collected until it gets unreachable again. According to the JavaDoc, finalize() will not be called again. 回答2: Then the object doesn't get garbage collected, basically. This is called object resurrection . Perform a search for that term, and you should get a

Why “Finalize method should not reference any other objects”?

岁酱吖の 提交于 2019-11-28 07:33:37
问题 I have been pondering why it is recommended that we should not release managed resources inside finalize. If you see the code example at http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx , and search for string "Dispose(bool disposing) executes in two distinct scenarios" and read that comment, you will understand what I mean. Only possibility I can think of is that it probably has something to do with the fact that it is not possible to predict when finalizer will get

Can I prevent an uncaught exception in another AppDomain from shutting down the application?

限于喜欢 提交于 2019-11-28 07:24:10
问题 I'm having trouble with a misbehaved library that throws an exception in a finalizer, which of course crashes the application. To avoid this, I tried loading the library in its own AppDomain, but the exception still bubbles to the surface and crashes the application. As documented on MSDN, registering to AppDomain.UnhandledException doesn't prevent the exception from bubbling up, but I'm quite surprised that there is no other way to catch such an exception in a "sub AppDomain". How do plugin

The difference between a destructor and a finalizer?

给你一囗甜甜゛ 提交于 2019-11-28 06:45:08
Please Note: This question is about the difference in terminology between the words "destructor" and "finalizer" and their correct usage. I have merely provided examples of their use in C# and C++/CLI to demonstrate why I am asking the question. I am well aware of how it is implemented in both C# and the CLR, but I am asking about the correct use of terminology. In the C# world the terms "destructor" and "finalizer" seem to be used pretty much interchangeably, which I suspect is because the C# specification describes the non-deterministic cleanup functionality using the word "destructor",