destructor

Destructor - does it get called if the app crashes

試著忘記壹切 提交于 2019-12-09 02:19:41
问题 Does a destructor get called if the app crashes? If it's an unhandled exception I'm guessing it does, but what about more serious errors, or something like a user killing the application process? And a few more potentially dumb questions: what happens to all the objects in an app when the app exits and all finalizers have been executed - do the objects get garbage collected or are they somehow all "unloaded" with the process or appdomain? is the garbage collector part of each application

Why do I need to delete pointers from vector manually?

[亡魂溺海] 提交于 2019-12-09 00:53:18
问题 Why do I need to delete dynamically created items in vector, manually? Why wouldn't they get deleted or its destructor called when vector got deleted? Normally something like this, but why needed ? vector<int*> v; for (vector<int*>::iterator it = v.begin(); it != v.end(); ++it) { delete *it; } 回答1: Firstly, you store raw pointers in your vector. These pointers are just pointers. They can point anywhere. They can point to local objects, which cannot be deleted by delete . And even if they

Is there C++ destructor equivalent in Java? [duplicate]

给你一囗甜甜゛ 提交于 2019-12-08 19:13:53
问题 This question already has answers here : Is there a destructor for Java? (21 answers) Closed 4 years ago . In simplest form, following is the design: class Session { Timer t = new Timer(); // ... }; Whenever, Session is allocated, I start a timer inside it; the timer will be expired after 10-20 mins. Now, suppose if Session is destroyed before the timer can expire; then it's a scenario where I must stop timer. I don't know if is there any last method which is always called when Session is

List destructor in C++

人盡茶涼 提交于 2019-12-08 18:55:29
I've just implemented the Linked List. It works perfectly fine but even tough I've seen notation I am unable to create working destructor on Node, that's why it's unimplemented here in code. I need to implement working destructor on node Destructor of List but this one is simple I will just use the destructor from Node class(but I need this one). Make the List friendly to Node so I will not have to use getNext(), but I think I can handle it myself(not sure how, but I'll find out). Please look at the code it is perfectly fine, just will work if you copy it. #include <cstdio> #include <cmath>

On Linux, why does the destructor run twice on shared instance of global variable in C++?

一笑奈何 提交于 2019-12-08 18:33:20
问题 On Linux I have some generated C++ code from a static library that defines a global variable. A single instance of this global variable is shared between two shared libraries that refer to its symbol. When the process shuts down and the static termination phase is run, I see that the destructor on this shared instance is run twice! Presumably once per library as each unloads. This question is closely related to another I saw recently here: related question. This sounds like the same behavior,

Why don't protected C++-Cli destructors cause compilation errors?

匆匆过客 提交于 2019-12-08 15:40:27
问题 If I compile and run the following: using namespace System; ref class C1 { public: C1() { Console::WriteLine(L"Creating C1"); } protected: ~C1() { Console::WriteLine(L"Destroying C1"); } }; int main(array<System::String ^> ^args) { C1^ c1 = gcnew C1(); delete c1; return 0; } ...the code compiles without an error and runs giving me this: Creating C1 Destroying C1 Press any key to continue . . . If I do the same in C++ I get an error along the following lines: 1>ProtectedDestructor.cpp(45):

Destructors not executed (no stack unwinding) when exception is thrown

淺唱寂寞╮ 提交于 2019-12-08 06:24:31
问题 I found a very very weird behaviour that I have never seen before. I'm working on a complex VS2005 C++ project. class Tester { public: Tester() { TRACE("Construct Tester"); } ~Tester() { TRACE("~Destruct Tester"); } }; void Thrower() { Tester X; throw std::exception("Booom"); } What do you expect to see in Trace output when Thrower() is called? That Tester is constructed and then destructed when the stack is unwinded, or not? At least I expect that, but the destructor of Tester is never

List destructor in C++

纵饮孤独 提交于 2019-12-08 05:35:57
问题 I've just implemented the Linked List. It works perfectly fine but even tough I've seen notation I am unable to create working destructor on Node, that's why it's unimplemented here in code. I need to implement working destructor on node Destructor of List but this one is simple I will just use the destructor from Node class(but I need this one). Make the List friendly to Node so I will not have to use getNext(), but I think I can handle it myself(not sure how, but I'll find out). Please look

C++ Access Violation while Reading from File

元气小坏坏 提交于 2019-12-08 04:26:18
问题 Just starting out on C++. I am getting access violation errors while reading from a binary file. Here are the classes involved: class Staff { //base class public: Staff() {} virtual ~Staff{} } One of the derived classes: class Scheduler : public Staff { public: Scheduler() {} //no destructor defined } And then in code that uses these classes: ifstream in("Scheduler.dat", ios::in | ios::binary); Scheduler s; in.read(reinterpret_cast<char *>(&s), sizeof(Scheduler)); The moment I hit the read

Call destructor of an element from a list

▼魔方 西西 提交于 2019-12-08 03:47:14
问题 I have something like that: a = [instance1, instance2, ...] if I do a del a[1] instance2 is removed from list, but is instance2 desctructor method called? I'm interested in this because my code uses a lot of memory and I need to free memory deleting instances from a list. 回答1: Coming from a language like c++ (as I did), this tends to be a subject many people find difficult to grasp when first learning Python. The bottomline is this: when you do del XXX , you are never* deleting an object when