destructor

Order and point of calling destructor

折月煮酒 提交于 2019-11-26 22:03:01
问题 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;

Why has the destructor been called only once?

淺唱寂寞╮ 提交于 2019-11-26 22:01:55
问题 #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... 回答1: 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

When will __destruct not be called in PHP?

我的梦境 提交于 2019-11-26 22:01:55
class MyDestructableClass { function __construct() { print "\nIn constructor\n"; $this->name = "MyDestructableClass"; } function __destruct() { print "\nDestroying " . $this->name . "\n"; } } $obj = new MyDestructableClass(); When the above script is in a complex environment,the __destruct won't get called when exit ,but I can't reproduce it easily.Have someone ever noticed this ? EDIT I'll post the whole stuff here,it's the testing environment of symfony,which means you can easily reproduce it if you are familar with the framework: require_once dirname(__FILE__).'/../bootstrap/Doctrine.php';

Non-trivial destructor make class non-trivially-constructible

喜夏-厌秋 提交于 2019-11-26 21:46:38
问题 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

Release Excel Object In My Destructor

自古美人都是妖i 提交于 2019-11-26 21:04:33
问题 I'm writing a Excel class using Microsoft.Interropt.Excel DLL. I finish all function but I have an error in my Destructor. I Want to save all changes to my file and I want to release all source. I want to all of them in my destructor. But In my destructor, Excel.ApplicationClass, Workbook and Worksheet objects are fill by an Exception which have message "COM object that has been separated from its underlying RCW cannot be used." So I can't save nothing, close nothing because ı can't access

C++ destruction of temporary object in an expression

删除回忆录丶 提交于 2019-11-26 21:04:15
问题 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

When is an object “out of scope”?

流过昼夜 提交于 2019-11-26 20:04:10
In C++, when is an object defined as "out of scope"? More specifically, if I had a singly linked list, what would define a single list node object as "out of scope"? Or if an object exists and is being referenced by a variable ptr , is it correct to say that the object is defined as "out of scope" the moment the reference is deleted or points to a different object? UPDATE: Assuming an object is a class that has an implemented destructor. Will the destructor be called the moment the object exits the scope? if (myCondition) { Node* list_1 = new Node (3); Node* list_2 = new Node (4); Node* list_3

What is the use of “delete this”?

一个人想着一个人 提交于 2019-11-26 19:57:56
问题 Today, I have seen some legacy code. In the destructor there is a statement like " delete this ". I think, this call will be recursive. Why it is working? I made some quick search on Y!, I found that if there is a need to restrict the user to create the stack object, we can make destructor private and provide an interface to delete the instance. In the interface provided, we have to call delete on this pointer. Are there any other situations for using such statements? 回答1: "delete this" is

Does delete on a pointer to a subclass call the base class destructor?

只谈情不闲聊 提交于 2019-11-26 19:14:59
I have an class A which uses a heap memory allocation for one of its fields. Class A is instantiated and stored as a pointer field in another class ( class B . When I'm done with an object of class B, I call delete , which I assume calls the destructor... But does this call the destructor of class A as well? Edit: From the answers, I take that (please edit if incorrect): delete of an instance of B calls B::~B(); which calls A::~A(); A::~A should explicitly delete all heap-allocated member variables of the A object; Finally the memory block storing said instance of class B is returned to the

GC.Collect() not collecting immediately?

ぐ巨炮叔叔 提交于 2019-11-26 18:36:57
问题 In the course of a discussion in chat, I wrote this console application. Code: using System; class Program { static void Main(string[] args) { CreateClass(); Console.Write("Collecting... "); GC.Collect(); Console.WriteLine("Done"); } static void CreateClass() { SomeClass c = new SomeClass(); } } class SomeClass { ~SomeClass() { throw new Exception(); } } Result: Collecting... Done Unhandled Exception: System.Exception: Exception of type 'System.Exception' was thrown. at SomeClass.Finalize() I