destructor

extraneous calls to copy-constructor and destructor

我怕爱的太早我们不能终老 提交于 2019-12-03 21:12:18
[a follow up to this question ] class A { public: A() {cout<<"A Construction" <<endl;} A(A const& a){cout<<"A Copy Construction"<<endl;} ~A() {cout<<"A Destruction" <<endl;} }; int main() { { vector<A> t; t.push_back(A()); t.push_back(A()); // once more } } The output is: A Construction // 1 A Copy Construction // 1 A Destruction // 1 A Construction // 2 A Copy Construction // 2 A Copy Construction // WHY THIS? A Destruction // 2 A Destruction // deleting element from t A Destruction // deleting element from t A Destruction // WHY THIS? To clearly see what's going on, I recommend include the

How does Rust know whether to run the destructor during stack unwind?

大城市里の小女人 提交于 2019-12-03 20:35:07
问题 The documentation for mem::uninitialized points out why it is dangerous/unsafe to use that function: calling drop on uninitialized memory is undefined behavior. So this code should be, I believe, undefined: let a: TypeWithDrop = unsafe { mem::uninitialized() }; panic!("=== Testing ==="); // Destructor of `a` will be run (U.B) However, I wrote this piece of code which works in safe Rust and does not seem to suffer from undefined behavior: #![feature(conservative_impl_trait)] trait T { fn disp(

Destructor not being called when leaving scope

雨燕双飞 提交于 2019-12-03 20:28:11
问题 I am learning memory management in C++ and I don't get the why only some of the destructors are called when leaving scope. In the code below, only obj1 destructor is called when myfunc ends, not for the dynamically allocated obj2. int myfunc (cl1 *oarg) { cout << "myfunc called" << std::endl; cl1 obj1(222,"NY"); cl1 *obj2; obj2= new cl1; oarg->disp(); obj2 -> ~cl1 ; } Here is the destructor I have : cl1 :: ~cl1 () { std::cout<< " cl1 destructor called"<<std::endl; std::cout << this->data <<

Opening a database connection in a constructor, when should I close it?

荒凉一梦 提交于 2019-12-03 17:22:39
well, I've been thinking of making database requests a little faster by keeping the connection to the database open as long as the object is being used. So I was thinking of opening the connection in the constructor of that class. Now the question is, how can I close the connection after I stopped using? I have to call close() somewhere, don't I? I've been reading about the finalize() method, but people seemed to be skeptical about usage of this method anywhere at all. I'd expect it to have something like a destructor, but Java doesn't have that, so? So could anyone provide me with a solution?

Python destructor basing on try/finally + yield?

﹥>﹥吖頭↗ 提交于 2019-12-03 17:20:20
I've been testing a dirty hack inspired by this http://docs.python.org/2/library/contextlib.html . The main idea is to bring try/finally idea onto class level and get reliable and simple class destructor. class Foo(): def __init__(self): self.__res_mgr__ = self.__acquire_resources__() self.__res_mgr__.next() def __acquire_resources__(self): try: # Acquire some resources here print "Initialize" self.f = 1 yield finally: # Release the resources here print "Releasing Resources" self.f = 0 f = Foo() print "testing resources" print f.f But it always gives me: Initialize testing resources 1 and

C++ stack allocated object, explicit destructor call

大城市里の小女人 提交于 2019-12-03 15:56:20
I came across a strange use of the destructor while working on an existing library. The destructor of a stack allocated stl vector was being called explicitly, when its the case that that object may need to be used again. These vector objects are a slightly customised version of the stl vector class that have a specialized clear method. In the destructor body there exist two method calls: clear() , _Tidy() . I've been trying to think of a good reason why this destructor is being called rather than just clear but I'm at a loss. Anyone shed any light on why this may be a good idea? clear() isn't

Do trivial destructors cause aliasing

断了今生、忘了曾经 提交于 2019-12-03 15:49:28
C++11 §3.8.1 declares that, for an object with a trivial destructor, I can end its lifespan by assigning to its storage. I am wondering if trivial destructors can prolong the object's lifespan and cause aliasing woes by "destroying an object" that I ended the lifespan of much earlier. To start, something which I know is safe and alias-free void* mem = malloc(sizeof(int)); int* asInt = (int*)mem; *asInt = 1; // the object '1' is now alive, trivial constructor + assignment short* asShort = (short*)mem; *asShort = 2; // the object '1' ends its life, because I reassigned to its storage // the

How can I call const member function from destructor

£可爱£侵袭症+ 提交于 2019-12-03 14:38:36
问题 Is there any possible way to invoke const member function from destructor, when const object is destroyed? Consider: struct My_type { ~My_type () { show (); } void show () { cout << "void show ()" << endl; } void show () const { cout << "void show () const" << endl; } }; And usage: My_type mt; const My_type cmt; mt.show (); cmt.show (); Output: void show () void show () const void show () void show () Can someone explain me why const version of show has not been invoked when cmt is destroyed?

Destructor that calls a function that can throw exception in C++

半腔热情 提交于 2019-12-03 12:58:21
I know that I shouldn't throw exceptions from a destructor. If my destructor calls a function that can throw an exception, is it OK if I catch it in the destructor and don't throw it further? Or can it cause abort anyway and I shouldn't call such functions from a destructor at all? Yes, that's legal. An exception must not escape from the destructor, but whatever happens inside the destructor, or in functions it calls, is up to you. (Technically, an exception can escape from a destructor call as well. If that happens during stack unwinding because another exception was thrown, std::terminate is

Transitioning to C++11 where destructors are implicitly declared with noexcept

浪尽此生 提交于 2019-12-03 12:37:56
In C++11, a destructor without any exception specification is implicitly declared with noexcept , which is a change from C++03. Therefore, a code which used to throw from destructors in C++03 would still compile fine in C++11, but will crash at runtime once it attempts throwing from such a destructor. Since there's no compile-time error with such a code, how could it be safely transitioned to C++11, short of declaring all and every existing destructor in the code base as being noexcept(false) , which would be really over-verbose and intrusive, or inspecting each and every destructor for being