destructor

Order and point of calling destructor

倾然丶 夕夏残阳落幕 提交于 2019-11-28 01:56:44
Lets say I have two local objects. When the function returns, is it guaranteed which one will go out of the scope first? For example: I have a class like this: class MutexLock { /* Automatic unlocking when MutexLock leaves a scope */ public: MutexLock (Mutex &m) { M.lock(); } ~MutexLock(Mutex &m) { M.unlock(); } }; This is a very common trick used to automatically release the mutex when going out of scope. But what if I need two mutexes in the scope? void *func(void *arg) { MutexLock m1; MutexLock m2; do_work(); } // m1 and m2 will get unlocked here. But in what order? m1 first or m2 first?

Why has the destructor been called only once?

时间秒杀一切 提交于 2019-11-28 01:52:41
#include <iostream> using namespace std; class Test { public: Test() { printf("construct ..\n"); } ~Test() { printf("destruct...\n"); } }; Test Get() { Test t = Test(); return t; } int main(int argc, char *argv[]) { Test t = Get(); return 0; } the console output is : $ g++ -g -Wall -O0 testdestructor.cc $ ./a.out construct .. destruct... I suppose the reason is return value optimization in 'Get'. Have a look at http://en.wikipedia.org/wiki/Return_value_optimization Actually your code is not the standard example, but maybe your compiler applies it here as well. Its because of copy-elision by

Is it OK to use “delete this” to delete the current object?

元气小坏坏 提交于 2019-11-28 01:09:05
I'm writing a linked list and I want a struct's destructor (a Node struct) to simply delete itself, and not have any side effects. I want my list's destructor to iteratively call the Node destructor on itself (storing the next node temporarily), like this: //my list class has first and last pointers //and my nodes each have a pointer to the previous and next //node DoublyLinkedList::~DoublyLinkedList { Node *temp = first(); while (temp->next() != NULL) { delete temp; temp = temp->next(); } } So this would be my Node destructor: Node::~Node { delete this; } Is this acceptable, especially in

Trivial Destructibility and Necessity of Calling Destructor

ε祈祈猫儿з 提交于 2019-11-28 01:03:07
问题 Suppose there exists a type T such that std::is_trivially_destructable<T>::value == true , and suppose further that T is the value type of some vector class. When the vector's destructor is called, or the vector is assigned to another vector, it must destroy and deallocate its current storage. As T is trivially destructable, is it necessary that I call T 's destructor? Thanks for your help! 回答1: According to the C++ Standard (section 3.8), you can end an object's lifetime by deallocating or

Non-trivial destructor make class non-trivially-constructible

和自甴很熟 提交于 2019-11-28 00:43:22
Consider following code: #include <type_traits> struct T {}; static_assert(std::is_trivially_destructible< T >{}); static_assert(std::is_trivially_default_constructible< T >{}); struct N { ~N() { ; } }; static_assert(!std::is_trivially_destructible< N >{}); static_assert(!std::is_trivially_default_constructible< N >{}); It compiles fine using clang 3.7.0 : live example . But summarizing the Standard : The default constructor for class T is trivial (i.e. performs no action) if all of the following is true: The constructor is not user-provided (i.e., is implicitly-defined or defaulted) T has no

Cleaning up an internal pysqlite connection on object destruction

左心房为你撑大大i 提交于 2019-11-28 00:31:06
问题 I have an object with an internal database connection that's active throughout its lifetime. At the end of the program's run, the connection has to be committed and closed. So far I've used an explicit close method, but this is somewhat cumbersome, especially when exceptions can happen in the calling code. I'm considering using the __del__ method for closing, but after some reading online I have concerns. Is this a valid usage pattern? Can I be sure that the internal resources will be freed

Is relying on __del__() for cleanup in python unreliable?

半世苍凉 提交于 2019-11-28 00:24:24
问题 I was reading about different ways to clean up objects in Python, and I have stumbled upon these questions (1, 2) which basically say that cleaning up using __del__() is unreliable and the following code should be avoid: def __init__(self): rc.open() def __del__(self): rc.close() The problem is, I'm using exactly this code, and I can't reproduce any of the issues cited in the questions above. As far as my knowledge goes, I can't go for the alternative with with statement, since I provide a

Is a Union Member's Destructor Called

╄→гoц情女王★ 提交于 2019-11-27 23:31:04
C++11 allowed the use of standard layout types in a union : Member of Union has User-Defined Constructor My question then is: Am I guaranteed the custom destructor will be called, when the union goes out of scope? My understanding is that we must manually destroy and construct when switching: http://en.cppreference.com/w/cpp/language/union#Explanation But what about an example like this: { union S { string str; vector<int> vec; ~S() {} } s = { "Hello, world"s }; } When s goes out of scope, have I leaked the memory the string allocated on the heap because I did not call string 's destructor?

What's the difference between “= default” destructor and empty destructor?

拟墨画扇 提交于 2019-11-27 22:43:33
I want to prevent the user of my class from using it as an automatic variable, so I write code like this: class A { private: ~A() = default; }; int main() { A a; } I expect that the code won't be compiled, but g++ compiles it without error. However, when I change the code to: class A { private: ~A(){} }; int main() { A a; } Now, g++ gives the error that ~A() is private, as is my expectation. What's the difference between a "= default" destructor and an empty destructor? Your first example should not compile. This represents a bug in the compiler that it does compile. This bug is fixed in gcc 4

C++ destruction of temporary object in an expression

谁说我不能喝 提交于 2019-11-27 22:37:33
Given the following code: #include <iostream> struct implicit_t { implicit_t(int x) : x_m(x) { std::cout << "ctor" << std::endl; } ~implicit_t() { std::cout << "dtor" << std::endl; } int x_m; }; std::ostream& operator<<(std::ostream& s, const implicit_t& x) { return s << x.x_m; } const implicit_t& f(const implicit_t& x) { return x; } int main() { std::cout << f(42) << std::endl; return 0; } I get the following output: ctor 42 dtor While I know this is correct, I'm not certain why . Is there anyone with stdc++ knowledge who can explain it to me? Temporary objects are destroyed as the last step