destructor

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

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! Pr...

Are there any instances when the destructor in PHP is NOT called?

Hello everyone! :) This is my first time posting to stackoverflow, but I these threads have helped me tremendously! Anywho, onto my question... are there any instances when the destructor in PHP is NOT called? The reason I ask is because I have a mapper class which maps data to objects and in the constructor, I start a transaction and ...

Indirectly destroying object from it's own virtual method. Is it a defined behavior?

Am I allowed to indirectly destroy object from within object's own virtual method? Is it a "defined behavior" (as long as I'm not trying to access anything after destroying the object)? Example: #include <memory> #include <stdio.h> using std::tr1::shared_ptr; struct Child{ virtual void selfdestruct() = 0; virtual ~Child(){ ...

Destructor crash

Hi, I am working on a Win32 c++ application in Visual studio. In one of the source files, I have global object like below. TestClass tObj; int main() //Execution starts here { } TestClass is defined in other DLL like below. struct Source { }; class TestClass { list<Source> sourceList; public: TestClass() {} ...

do i need virtual destructor for boost::ublas matrix ?

do i need virtual destructor when iam using boost::ublas matrix ? btw my class is a template class ...

C# Destructor not working as expected

Hi, Please see the code below. I expect it to print either 10 because I have explicitly invoked the garbage collector. But I always get either a 0 or 20 as output. Why is that? void Main() { Panda[] forest_panda = new Panda[10]; for(int i=0; i<forest_panda.GetLength(0);i++) { forest_panda[i]=new Panda("P1"); } ...

Should an abstract class' destructor be pure virtual?

I think virtual alone is generally sufficient. Is there another reason to make it pure virtual than to force derived classes to implement their own destructor? I mean if you allocate something in your class' constructor you should impement your own destructor - if your class is derived or not. Doesn't count as answer as I already know:...

Can I destruct a structure in C++?

Is there a way to destruct a structure (not a class)? ...

Why should I use Free and not FreeAndNil in a destructor ?

I have read A case against FreeAndNil but still don't understand why I cannot use this method in a class destructor ? Can anyone explain. Update: I think the comment from Eric Grange was most useful for me. The link show that this is not obvious how to deal with it and it is mainly a matter of taste. Also the method FreeAndInvalidate wa...

Vectors within classes: handeling copy constructor and destructor (C++)

Take a simple class with the "big 3" (constructor, copy constructor, destructor): #include <vector> using namespace std; //actually goes in the C file that links to this header file ... class planets(){ //stores mass and radii data for planets in a solar system. public: vector <double> mass; vector <double> radius; //...

Why is a control destroyed twice when it's nested in a tab control?

I've been trying to debug why closing windows forms designer is crashing visual studio and have discovered by adding a series of message boxes that if my controls are nested inside a System::Windows::Forms::TabControl the destructor of those controls are called twice. Is that expected behaviour and is there a way of avoiding it? ...

constructors and destructors - c++

Hi, I need to write a program that prints 100 stars on the screen (at random places), and then the stars disappear slowly - one after another. I'm not allowed to use loops nor recursions. I've tried to play with the constructors and the destructors but I can't get the stars to disappear one after another (and not all together). Any idea...

Qt C++ destructor taking a long time to return

I'm working on a pretty standard Qt mobile app (written in C++, targeted at Symbian devices), and am finding that sometimes when the app is closed (i.e. via a call to QApplication::quit), the final destructor in the app can take a long time to return (30 seconds plus). By this I mean, all clean up operations in the destructor have comple...

Why does this virtual destructor trigger an unresolved external?

Consider the following: In X.h: class X { X(); virtual ~X(); }; X.cpp: #include "X.h" X::X() {} Try to build this (I'm using a .dll target to avoid an error on the missing main, and I'm using Visual Studio 2010): Error 1 error LNK2001: unresolved external symbol "private: virtual __thiscall X::~X(void)" (??1X@@EAE@XZ...

c++ Inheritance: Destructor does not get called...

I got my code like the following: class TimeManager { public: virtual ~TimeManager(); }; class UserManager : virtual public TimeManager { public: virtual ~UserManager(); }; class Server : virutal public UserManager { virtual ~Server(); }; CServer *pServer; DWORD WINAPI ServerHelper(void*); int main() { //Create server CreateT...

C++: Best way to destruct static stuff

Hi, When I have a class containing static stuff, how can I free the memory at the end of the application the best way? Foo.h class GLUtesselator; class Foo { private: static GLUtesselator *tess; public: Foo(); virtual ~Foo(); } Foo.cpp #include "Foo.h" #include <GL/glu.h> GLUtesselator *Foo::tess = gluNewTess(); // S...

C++ Too many destructors called so few objects

Here is the code (also at http://pastebin.com/yw5z2hnG ): #include <iostream> #include <vector> using namespace std; class X { public: int i; X(); ~X(); }; X::X() { i = 1; cout << "---constructor" << '\n'; } X::~X() { cout << "***desctructor" << '\n'; } int main() { vector<X> *vx = new vector<X>; ...

base class 'class std::vector<...>' has a non-virtual destructor

One of my C++ classes derives from std::vector so that it can act as a container that also perform custom actions on its content. Unfortunately, the compiler complains about the destructor not to be virtual, which I cannot change, since its in the standard library. Am I doing the whole thing wrong (thou shall not derive from STL) or is ...

Side effect / Volatile / Copy Constructor / Destructor

With reference to the discussion here $3.7.1/2 - "If an object of static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, except that a class object or its copy may be eliminated as specified in 12.8." $12.8/15- "When certain criteria are met, an implemen...

Reason reqd for the order of destructor call .?

As I have read in certain forums,when derived class object is created base class members and methods are allocated space in the memory but there is no specific base class object. Now as the derived class object goes out of scope , why derived class destructor is called first.What is the constraint of the compiler where derived class des...