destructor

Do I have to set pointer to nullptr in destructor?

Possible Duplicate: Is it worth setting pointers to NULL in a destructor? Do I have to do something like so: ~MyClass() { delete[] my_data_; my_data_ = nullptr;//DO I HAVE TO WRITE THIS LINE? } Thank you. ...

Does destructor of a C++ class that throws an exception gets called?

Suppose I have a class like this: #include <iostream> using namespace std; class Boda { private: char *ptr; public: Boda() { ptr = new char [20]; } ~Boda() { cout << "calling ~Boda\n"; delete [] ptr; } void ouch() { throw 99; ...

making a python object unsable after a finalize-type call

I have a python object which wraps a sensitive and important resource on the system. I have a cleanup() function which safely releases various locks used by the object. I want to make sure that after a call to cleanup() the object becomes unusable. Ideally, any call to any member function of the object would raises an exception. Is the...

Do I need to specify virtual on the sub-classes methods as well ?

This has probably been asked before on SO, but I were unable to find a similar question. Consider the following class hierarchy: class BritneySpears { public: virtual ~BritneySpears(); }; class Daughter1 : public BritneySpears { public: virtual ~Daughter1(); // Virtual specifier }; class Daughter2 : public BritneySpears...

Under what circumstances are C++ destructors not going to be called?

I know that my destructors are called on normal unwind of stack and when exceptions are thrown, but not when exit() is called. Are there any other cases where my destructors are not going to get called? What about signals such as SIGINT or SIGSEGV? I presume that for SIGSEGV, they are not called, but for SIGNINT they are, how do I kno...

Goto out of a block: do destructors get called ?

Consider the following code: void foo() { { CSomeClass bar; // Some code here... goto label; // and here... } label: // and here... } Will the destructor of bar be called ? ...

Deleting nodes in a doubly linked list (C++)

I have problems understanding why when I create two or more nodes (as shown below), the function void del_end()will only delete the char name[20] and not the whole node . How do I fix this problem without memory leak? #include <iostream> using namespace std; struct node { char name[20]; char profession[20]; int age; no...

C++: Why is the destructor being called here?

I guess I don't fully understand how destructors work in C++. Here is the sample program I wrote to recreate the issue: #include <iostream> #include <memory> #include <vector> using namespace std; struct Odp { int id; Odp(int id) { this->id = id; } ~Odp() { cout << "Destructing Odp " << id << ...

How does an exception specification affect virtual destructor overriding?

The C++ Standard states the following about virtual functions that have exception specifications: If a virtual function has an exception-specification, all declarations, including the definition, of any function that overrides that virtual function in any derived class shall only allow exceptions that are allowed by the exception-spe...

PHP classes and static variables - Should I use __destruct() ?

I have a class that's similar to the following: class Person { private static $_sqlData; public function __construct($id) { if (!self::$_sqlData) { self::$_sqlData = // GET THE DB STUFF } } public function getName() { return self::$_sqlData['name']; } } This has bee...

Why base class destructor (virtual) is called when a derived class object is deleted?

A difference between a destructor (of course also the constructor) and other member functions is that, if a regular member function has a body at the derived class, only the version at Derived class gets executed. Whereas in case of destructors, both derived as well as base class versions get executed? It will be great to know what exac...

Does explicitly calling destructor result in Undefined Behavior here?

In my opinion, the following code (from some C++ question) should lead to UB, but the it seems it is not. Here is the code: #include <iostream> using namespace std; class some{ public: ~some() { cout<<"some's destructor"<<endl; } }; int main() { some s; s.~some(); } and the answer is: some's destructor some's destructor I learned f...

C++ Base class destrutor order problem

Does anyone know any trick I could use to keep the Derived class until the base class destructor have been called? i.e: #include <iostream.h> class Base { public: Base(){ cout<<"Constructor: Base"<<endl;} virtual ~Base(){ cout<<"Destructor : Base"<<endl;} }; class Derived: public Base { //Doing a lot of ...

(Ab)using constructors and destructors for side effects bad practice? Alternatives?

In OpenGL, one often writes code like this: glPushMatrix(); // modify the current matrix and use it glPopMatrix(); Essentially, the state is changed, then some actions are performed that use the new state, and finally the state is restored. Now there are two problems here: It's easy to forget to restore the state. If the code in be...

When a window closes, do my destructors get called?

If a window is closed (like with sending WM_CLOSE), are the destructors of objects called? I followed my source code with a break point in that situation, but the compiler doesn't seem to pass through my destructor. Is the program closed without calling any destructors? ...

Way for C++ destructor to skip work when specific exception being thrown?

I have an object on the stack for which I wish its destructor to skip some work when the destructor is being called because the stack is being unwound due to a specific exception being thrown through the scope of the object on the stack. Now I could add a try catch block inside the scope of the stack item and catch the exception in ques...

Is destructor in PHP predictable?

Is a class destructor in PHP predictable? When is the destructor called? Like in many languages, will a class destructor be called as soon as the object goes out of scope? ...

virtual desctructor on pure abstract base class

I have struct IMyInterface { virtual method1() = 0; virtual method2() = 0; }; GCC insists that I have struct IMyInterface { virtual method1() = 0; virtual method2() = 0; virtual ~IMyInterface(){}; }; I dont see why. A pure interface is all about the interface (duh). The destructor is part of the internal implementati...

Why is vector deleting destructor being called as a result of a scalar delete?

I have some code that is crashing in a large system. However, the code essentially boils down to the following pseudo-code. I've removed much of the detail, as I have tried to boil this down to the bare bones; I don't think this misses anything crucial though. // in a DLL: #ifdef _DLL #define DLLEXP __declspec(dllexport) #else #define ...

Moving ctor and moving dtor.

Hi, as I've asked in here and after a while I've agreed and accepted right answer to that question I was just thinking, if would it be useful to have something like "moving destructor" which would be invoked on moved object everytime we used move ctor or operator=. In this way we would have to specify only in move dtor what we want from...