finalizer

Can I override Dispose to make an entity class that always calls SaveChanges?

你离开我真会死。 提交于 2019-12-04 12:06:02
This is a fairly fine point, and I expect the answer is "it's not a hot idea to begin with" - that said, it has a points that I'm interested in regardless, if someone is kind enough to indulge. Model Code: public partial class MyEntities : ObjectContext { // the idea is if the object is in a using block, this always gets called? protected override void Dispose(bool disposing) { this.SaveChanges(); base.Dispose(disposing); } } Client Code: using(var model = new MyEntities()) { // do something // no worry about calling model.SaveChanges() } The issues I'm uncertain about are: Is Dispose the

Why .NET Object has method Finalize()?

烈酒焚心 提交于 2019-12-04 09:36:09
I know that Finalize method is used by garbage collector to let object free up unmanaged resources. And from what I know, Object.Finalize is never called directly by GC (object is added to f-reachable queue during it's construction if it's type overrides the Finalize method by implementing finalizer). Object.Finalize is only called from autogenerated finalizer code : try { //My class finalize implementation } finally { base.Finalize(); // Here chain of base calls will eventually reach Object.Finalize/ } So having an arbitrary class, derived from Object, wouldn't call Object.Finalize - you need

IDisposable, Finalizers and the definition of an unmanaged resource

不打扰是莪最后的温柔 提交于 2019-12-04 08:40:17
I'm trying to make sure that my understanding of IDisposable is correct and there's something I'm still not quite sure on. IDisposable seems to serve two purpose. To provide a convention to "shut down" a managed object on demand. To provide a convention to free "unmanaged resources" held by a managed object. My confusion comes from identifying which scenarios have "unmanaged resources" in play. Say you are using a Microsoft-supplied IDisposable -implementing (managed) class (say, database or socket-related). How do you know whether it is implementing IDisposable for just 1 or 1&2 above? Are

Why are there finalizers in java and c#?

天涯浪子 提交于 2019-12-04 02:53:01
I'm not quite understanding why there are finalizers in languages such as java and c#. AFAIK, they: are not guaranteed to run (in java) if they do run, they may run an arbitrary amount of time after the object in question becomes a candidate for finalization and (at least in java), they incur an amazingly huge performance hit to even stick on a class. So why were they added at all? I asked a friend, and he mumbled something about "you want to have every possible chance to clean up things like DB connections", but this strikes me as a bad practice. Why should you rely on something with the

Very strange OutOfMemoryError

旧时模样 提交于 2019-12-04 02:18:51
As always, a lengthy problem description. We are currently stress testing our product - and we now we face a strange problem. After one to two hours, heap space begins to grow, the application dies sometime later. Profiling the application shows a very large amount of Finalizer objects, filling the heap. Well, we thought "might be the weird finalizer thread to slow" issue and reviewed for reducing the amount of objects that need to be finalized (JNA native handles in this case). Good idea anyway and reduced thousands of new objects... The next tests showed the same pattern, only one hour later

When would dispose method not get called?

喜你入骨 提交于 2019-12-04 01:24:14
问题 I was reading this article the other day and was wondering why there was a Finalizer along with the Dispose method. I read here on SO as to why you might want to add Dispose to the Finalizer. My curiousity is, when would the Finalizer be called over the Dispose method itself? Is there a code example or is it based on something happening on the system the software is running? If so, what could happen to not have the Dispose method run by the GC. 回答1: The purpose of the finaliser here is simply

F# Equivalent of Destructor

不想你离开。 提交于 2019-12-04 00:28:45
I am translating a C# class that wraps an unmanaged library to F#. I have run into the seemingly simple problem of rewriting the destructor that follows. class Wrapper { // P/Invoke ellided private SomeType x; public Wrapper() { x = new SomeType(); Begin(); } public ~Wrapper() { End(); } The simplified F# code I have at this point is as follows: type Wrapper() = [<Literal>] static let wrappedDll = "Library.dll" [<DllImport(wrappedDll , EntryPoint = "Begin")>] static extern void Begin() [<DllImport(wrappedDll , EntryPoint = "End")>] static extern void End() let x = new SomeType() do Begin() How

What is the scope of finalizer thread - per application domain or per process?

跟風遠走 提交于 2019-12-03 16:14:13
问题 Based on all my reading there should be one GC thread to invoke all finalizers. Now, the question is what is the scope of this "one" thread - per process or per application domain, as the whole intention of domains is to separate and make "independent" different applications in one process space. I read here: If an unhandled exception occurs in a finalizer the CLR's executing thread will swallow the exception, treat the finalizer as if it completed normally, remove it from the freachable

Which objects are finalized in Go by default and what are some of the pitfalls of it?

核能气质少年 提交于 2019-12-03 15:02:10
The function runtime.SetFinalizer (x, f interface{}) sets the finalizer associated with x to f . What kind of objects are finalized by default? What are some of the unintended pitfalls caused by having those objects finalized by default? The following objects are finalized by default: os. File : The file is automatically closed when the object is garbage collected. os. Process : Finalization will release any resources associated with the process. On Unix, this is a no-operation. On Windows, it closes the handle associated with the process. On Windows, it appears that package net can

Why does SHA1.ComputeHash fail under high load with many threads?

三世轮回 提交于 2019-12-03 12:33:28
I'm seeing an issue with some code I maintain. The code below has a private static SHA1 member (which is an IDisposable but since it's static , it should never get finalized). However, under stress this code throws an exception that suggests it has been closed: Caught exception. Safe handle has been closed" Stack trace: Call stack where exception was thrown at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success) at System.Security.Cryptography.Utils.HashData(SafeHashHandle hHash, Byte[] data, Int32 cbData, Int32 ibStart, Int32 cbSize) at System.Security.Cryptography