destructor

Should every class have a virtual destructor?

Java and C# support the notion of classes that can't be used as base classes with the final and sealed keywords. In C++ however there is no good way to prevent a class from being derived from which leaves the class's author with a dilemma, should every class have a virtual destructor or not? On the one hand giving an object a virtual d...

Destructor question C++

I declared a private variable vector<SomeClass> theVector; someplace inside my SomeClass class. Why can't I say: "delete theVector" inside my SomeClass destructor? The compiler error says: type `class Vector<SomeClass>' argument given to `delete', expected pointer What expected pointer? ...

Python: flush a buffer before program termination via a finalizer

I keep a cache of transactions to flush (to persistent storage) on the event of a watermark or object finalization. Since __del__ is no longer guaranteed to be called on every object, is the appropriate approach to hook a similar function (or __del__ itself) into atexit.register (during initialization)? If I'm not mistaken, this will ca...

Buffer overrun in 1 line ! (uses the PCRE library)

Well, if someone can help that would be nice...cause I'm almost going nuts ! I only have 1 line of code, and this is : pcrecpp::RE re("abc"); inside a function OnBnClickedButtonGo(). And this function fails in the Release Mode ..! ..while it works OK in debug mode. (I use VS 8 on WinXP) The error message is : "A buffer overrun has oc...

About constructors/destructors and new/delete operators in C++ for custom objects.

Suppose I have a Linked List I created myself. It has its own destructor, which frees the memory. This Linked List does not overload new or delete. Now, I'm trying to create an array of said linked lists (open hashing, if I understand correctly). Then I allocate the necessary memory inside the constructor of this open hashing class. The...

destructors: triviality vs implicit definition

As I understand the standard, a trivial destructor is one which is implicitly declared and whose class has only base and non-static members with trivial destructors. Given the recursivity of this definition, it seems to me that the only "recursion-stopping" condition is to find a base or non-static member with a non-implicitly declared d...

Destructors of builtin types (int, char ect..)

In C++ the following code gives a compiler error: void destruct1 (int * item) { item->~int(); } This code is nearly the same, I just typedef the int to another type and something magic happends: typedef int myint; void destruct2 (myint * item) { item->~myint(); } Why does the second code works? Does an int gets a destructor ju...

Should I add throw() to the declarations for my C++ destructors?

I have seen some C++ classes with a destructor defined as follows: class someClass { public: someClass(); ~someClass() throw(); }; Is this a good idea? I am well aware that destructors should never throw exceptions, but will this actually prevent me from throwing exceptions in my destructors? I'm not 100% sure wha...

When to use virtual destructors?

Hi, I have a solid understanding of most OO theory but the one thing that confuses me a lot is virtual destructors. I thought that the destructor always gets called no matter what and for every object in the chain. When are you meant to make them virtual and why? ...

Destructor vs member function race

When I'm inside a destructor is it possible that some other thread will start executing object's member function? How to deal with this situation? ...

Why is the destructor ignored in this C++ code? (Turbo C++ Explorer, Borland C++ Builder 2006)

The following code demonstrates a weird problem I have in a Turbo C++ Explorer project. One of the three stack objects in D::D() is not destroyed after going out of scope. This only happens if compiled in release mode, the auto_ptrs a_ and b_ are of different types and the exception thrown doesn't inherit from std::exception. It appear...

Weird behaviour of C++ destructors

#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000, vector<int>(4, -1)); cout << dp.size(); } This tiny program takes a split second to execute when simply run from the command line. But when run in a debugger, it takes over 8 seconds. Pausing the debugger reveals that it is ...

Excel VBA object constructor and destructor

I need to make some custom objects in VBA that will need to reference each other and I have a some issues. First - how do object constructors work in VBA? Are there constructors? Second - are there destructors? How does VBA handle the end of the object lifecycle? If I have an object that references others (and this is their only refere...

C : pthread dataspecific destructor called only once

From pthread_key_create manpage : An optional destructor function may be associated with each key value. At thread exit, if a key value has a non-NULL destructor pointer, and the thread has a non-NULL value associated with the key, the function pointed to is called with the current associated value as its sole argume...

Why isn't my .net destructor called in this very simple scenario?

I've got the following code : public class A { ~A() { Console.WriteLine("destructor"); } } public static A Aref; static void Main(string[] args) { Aref = new A(); int gen = GC.GetGeneration(Aref); Aref = null; GC.Collect(gen, GCCollectionMode.Forced); ...

is it legal to recreate a rooted reference to 'this' in a .net destructor ?

Is it legal to write the following in .net ? public class A { public int i = 0; ~A() { Aref = this; } } public static A Aref; static void Main(string[] args) { Aref = new A(); int gen = GC.GetGeneration(Aref); Aref = null; GC.Collect(gen...

Costs involved with C# destructors (aka: finalizers)?

The destructor should only release unmanaged resources that your object holds on to, and it should not reference other objects. If you have only managed references you do not need to (and should not) implement a destructor. You want this only for handling unmanaged resources. Because there is some cost to having a destructor, you ough...

What is the use of having destructor as private?

What is the use of having destructor as private? ...

Should destructors be threadsafe?

I was going through a legacy code and found the following snippet: MyClass::~MyClass() { EnterCriticalSection(&cs); //Access Data Members, **NO Global** members are being accessed here LeaveCriticalSection(&cs); } I am wondering will it help by any chance to guard the destructor ? Consider a scenario : 1. Thread1 - About to...

What is the order in which the destructors and the constructors are called in C++

What is the order in which the destructors and the constructors are called in C++? Using the examples of some Base classes and Derived Classes ...