destructor

Why garbage collector takes objects in the wrong order?

眉间皱痕 提交于 2019-11-28 07:43:57
问题 I have an application with two classes, A and B. The class A has inside a reference to class B. The destructors of the classes do some cleanup of resources but they have to be called in the right order, first the destructor of A and then the destructor of B. What is happening is that somehow the destructor of B is called first and then the destructor of A is crashing because is trying to execute methods from a disposed object. Is this behavior of the GC correct? I expected the GC to detect

Are locals destroyed before or after evaluation of a function return value?

一个人想着一个人 提交于 2019-11-28 07:31:37
问题 I am thinking of making a class which represents ownership of a synchronization primitive, something like this: class CCriticalSectionLock { public: CCriticalSectionLock( CCriticalSection &cs ) : cs( cs ) { cs.Enter(); } ~CCriticalSectionLock() { cs.Leave(); } private: CCriticalSection &cs; }; This looks like a good way to be able to take ownership during a function and ensure ownership is released even if there are multiple exit points or exceptions. It does, however, raise some subtle

What happens to base class destructor if a derived class destructor throws an exception

陌路散爱 提交于 2019-11-28 06:51:09
It just happened to me I wondered how resources are freed in the following case. class Base { Resource *r; public: Base() { /* ... */ } ~Base() { delete r; } }; class Derived : public Base { public: Derived() { /* ... */ } ~Derived() { /* Suddenly something here throws! */ } }; int main() { try { Derived d; } catch(...) { /* what happened with Base::r !? */ } } Will the base class destructor be called if the derived class destructor throws? Or will there be a leak? GManNickG According to §15.2/2: An object that is partially constructed or partially destroyed will have destructors executed for

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",

Saving a Class to disk on dispose: Does my code have bugs?

好久不见. 提交于 2019-11-28 05:36:20
问题 I am attempting to make a simple class that serializes itself to disk when it is no longer in use. The code I have right now (see below). The code I have now seems to work, but I am not fully confident in my knowledge, so I am wondering if anyone else sees any significant problems with this code. void IDisposable.Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~MyClass() { Dispose(false); } protected virtual void Dispose(bool disposing) { if (!this.disposed) { MemoryStream ms = new

Static Finalizer

核能气质少年 提交于 2019-11-28 04:39:34
What is the right way to perform some static finallization? There is no static destructor. The AppDomain.DomainUnload event is not raised in the default domain. The AppDomain.ProcessExit event shares the total time of the three seconds (default settings) between all event handlers, so it's not really usable. Basically, you can't. Design your way around it to the fullest extent possible. Don't forget that a program can always terminate abruptly anyway - someone pulling out the power being the obvious example. So anything you do has to be "best effort" - in which case I'd certainly hope that

PHP file creation/write within destructor

允我心安 提交于 2019-11-28 04:16:53
问题 When calling file_put_contents() within a destructor, it causes files to be written in SERVER_ROOT ... (Yikes!) Workarounds? tldr: I want to cache an array, probably containing serialized class instances. I figured, for now, I would write a class that implements the cache using unserialize()/file_get_contents() and serialize()/file_put_contents() and then hide its functionality behind a more generic Cache class. (I don't know if my client's host will have shared memory or PEAR, etc) <?php

What is the order in which the destructors and the constructors are called in C++

梦想的初衷 提交于 2019-11-28 03:54:55
What is the order in which the destructors and the constructors are called in C++? Using the examples of some Base classes and Derived Classes Brian R. Bondy The order is: Base constructor Derived constructor Derived destructor Base destructor Example: class B { public: B() { cout<<"Construct B"<<endl; } virtual ~B() { cout<<"Destruct B"<<endl; } }; class D : public B { public: D() { cout<<"Construct D"<<endl; } virtual ~D() { cout<<"Destruct D"<<endl; } }; int main(int argc, char **argv) { D d; return 0; } Output of example: Construct B Construct D Destruct D Destruct B Multiple levels of

Are destructors overloadable?

痞子三分冷 提交于 2019-11-28 02:48:47
问题 enable_if doc page says: Constructors and destructors do not have a return type; an extra argument is the only option. Are destructors overloadable? 回答1: No 回答2: Are destructors overloadable? The answer is plain No . Two versions of desturctor cannot co-exist in a class body. However unlike the popular belief, note that destructor does have 2 syntax. struct E { ~E(); // syntax-1 ~E() throw(); // syntax-2 }; Syntax-2 is less popular. But it is mandatory, if the base class destructor contains

Javascript's equivalent of destruct in object model [duplicate]

你。 提交于 2019-11-28 02:24:40
问题 This question already has an answer here: When are JavaScript objects destroyed? 4 answers Since I've dealt in the past with javascript's funky "object model", I assume there is no such thing as a destructor. My searches were mildly unsuccessful, so you guys are my last well of hope. How do you execute stuff upon instance destruction? 回答1: MDN is a nice resource for JS. No, there is nothing like calling a function when an object ceases. 来源: https://stackoverflow.com/questions/22566307