destructor

Do I need to explicitly call the base virtual destructor?

When overriding a class in C++ (with a virtual destructor) I am implementing the destructor again as virtual on the inheriting class, but do I need to call the base destructor? If so I imagine it's something like this... MyChildClass::~MyChildClass() // virtual in header { // Call to base destructor... this->MyBaseClass::~MyBas...

Does delete call the destructor in C++?

I have an class (A) which uses a heap memory allocation for one of it's fields. Class A is instantiated and stored as a pointer field in another class (B). When I'm done with object B, I call delete, which I assume calls the destructor... But does this call the destructor in class A as well? Edit: From the answers, I take that (pleas...

Why is the destructor not called for the returned object from the function?

I was thinking that when a function returns an object on the stack to the calling function, the calling function gets a copy of the original object but the original object's destructor is called as soon as the stack unwinds. But in the following program, the destructor is getting called only once. I expected it to be called twice. #incl...

Static instance, desctructor never called

Please see code below. The destructors are never called. Anyone know why and how this can be rectified? public partial class Form1 : Form { private Goo goo; public Form1() { InitializeComponent(); goo = Goo.GetInstance(); } } public class Goo { private foo f = new foo(); private sta...

Acceptable answers for an interview question

What kind of answers would you accept to the following question "Describe the process and/or pitfall of throwing exceptions from constructor and destructors" (C++/C#/java) What amount of knowledge about this would you consider essential, for a candidate claiming to have several years of experience in any of these languages (if he misse...

c++ compiling error related to constructor/destructor definition.

Hello, I'm trying to define the constructor and destructor of my class but I keep getting the error: definition of implicitly-declared 'x::x()'. What does it mean? BestWishes! Part of the code: ///Constructor StackInt::StackInt(){ t = (-1); stackArray = new int[20]; }; ///Destructor StackInt::~StackInt(){ delete[] stackArr...

How do I cleanup an opened Process in Java?

I'm starting a Process from a Java program. I hold onto it and at later points in the program I may send it some signals (not as in UNIX signals -- a different mechanism) to tell it to clean itself up and shut down, which is the proper way of terminating this process. I may later restart and hold onto the process and stop it again an a...

Virtual Default Destructors in C++

Hello all :) I've got a large set of inherited classes (criteria) which inherit from a base class (criterion). Here's criterion's code class criterion { public: virtual unsigned __int32 getPriorityClass() const = 0; virtual BOOL include(fileData &file) const = 0; virtual void reorderTree() = 0; virtual unsigned int dire...

Throwing Destructors, Memory Corruption?

We have a class whose semantic behaviour is like the following :- struct Sample { ~Sample() throw() { throw 0; } }; void f () { try { delete new Sample; } catch (...){ } } I know that throwing exceptions in dtors is evil; but the relinquishment of a 3rd Party library resource is throwing an exception (but can...

How do I correctly clean up a Python object?

class Package: def __init__(self): self.files = [] # ... def __del__(self): for file in self.files: os.unlink(file) __del__(self) above fails with an AttributeError exception. I understand Python doesn't guarantee the existence of "global variables" (member data in this context?) when __del__(...

How to force destruction order of static objects in different dlls?

I have 2 static objects in 2 different dlls: An object Resources (which is a singleton), and an object User. Object User in its destructor has to access object Resources. How can I force object Resources not to be destructed before object User? ...

C++ destructors question

With regards to the sample code below, why is the destructor for the base class called twice? class Base { public: Base() { std::cout << "Base::Base()" << std::endl; } ~Base() { std::cout << "Base::~Base()" << std::endl; } }; class Derived : public Base { public: Derived() { std::cout << "Derived::...

WPF Destructor

Does anyone know the correct method for writing a destructor for an XBAP application? ...

Method that gets called on module deletion in Python

Is there a method that I can add to my module, which will get called when destructing the class? Update: We have a simple class which has only static member functions and needs to clean up the database connection when unloading the module. was hoping there would be a __del__ method either for modules or classes that don't have instances...

Cleaning up an internal pysqlite connection on object destruction

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 __de...

Question about pure virtual destructor.

If we define a abstract class which has a pure virtual destructor, why do we have to give a definition of a destructor in the abstract class? ...

Why WCF does not destroy the object when I close the client application without calling "Close" method on the client side?

I have a net tcp WCF service as follows [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class AVService : IAVService { static int _numberofInst = 0; public AVService() { ++_numberofInst; Console.WriteLine("Number of instances "+_numberofInst); ...

COM-like interfaces warn about non virtual destructor

Is there a way to tell gcc that the abstract class it's compiling does not need a virtual destructor (like COM-objects never have)? For example nsISupports always complains about the missing virtual destructor. Turning off the warning would not help as I may have non-COM-like classes, where I want this warning. So __attribute__((com_int...

C++: Will an 'empty' destructor do the same thing as the generated destructor?

Suppose we have a (toy) C++ class such as the following: class Foo { public: Foo(); private: int t; }; Since no destructor is defined, a C++ compiler should create one automatically for class Foo. If the destructor does not need to clean up any dynamically allocated memory (that is, we could reasonably rely on...

Does calling a destructor explicitly destroy an object completely?

If I call a destructor explicitly ( myObject.~Object() ) does this assure me that the object will be appropriately destroyed (calling all child destructors) ? Ok some code: class Object { virtual ~Object() {} }; class Widget : public Object { virtual ~Widget() {} }; ... Object* aWidget = new Widget(); //allocate and cons...