destructor

A destructor - should I use delete or delete[]?

I am writing a template class that takes as an input a pointer and stores it. The pointer is meant to point to an object allocated by another class, and handed to the this containing class. Now I want to create a destructor for this container. How should I free the memory pointed to by this pointer? I have no way of knowing a priori wh...

C++ destructor & function call order.

Suppose I have the following snipplet: Foo foo; .... return bar(); Now, does the C++ standard guarantees me that bar() will be called before foo::~Foo() ? Or is this the compiler/implementation's choice? Thanks! ...

Are virtual destructors inherited?

If I have a base class with a virtual destructor. Has a derived class to declare a virtual destructor too? class base { public: virtual ~base () {} }; class derived : base { public: virtual ~derived () {} // 1) ~derived () {} // 2) }; Concrete questions: Is 1) and 2) the same? Is 2) automatically virtual because of its...

How can I control the order heap objects are destroyed?

Let's say I have 2 singletons, allocated on the heap, for which no delete is ever called. Let's call them A and B. Is there any way to make sure B will be the first one to be destroyed? I'm assuming the platform may matter on this one: Visual Studio 2005 Professional, Visual C++. Everything was built with cl. ...

Looking for the name of a design pattern

VBScript guarantees that the GC will run after every line, so if you create an object and don't keep a reference, its destructor will be called at the end of the line. This allows you to do a number of interesting things, one of which is simulating optional arguments: with foo(mandatoryArg) .optArg = 42 end Another is allowing for...

Does C++ call destructors for global and class static variables?

From my example program, it looks like it does call the destructors in both the cases. At what point does it call the destructors for global and class-static variables since they should be allocated in the data section of the program stack? ...

One Valid Case of Exception leaving a Destructor

I'm working on a simple class to manage the lifetime of a HKEY. class Key { HKEY hWin32; public: Key(HKEY root, const std::wstring& subKey, REGSAM samDesired); Key(const Key& other); ~Key(); Key& operator=(const Key& other); Key& swap(Key& other); HKEY getRawHandle() { return hWin32; }; }; //Other Methods.....

c++: following piece of code crashes

#include <iostream> using namespace std; class B { public: B() { cout << "Base B()" << endl; } ~B() { cout << "Base ~B()" << endl; } private: int x; }; class D : public B { public: D() { cout << "Derived D()" << endl; } virtual ~D() { cout << "Derived ~D()" << endl; } }; int main ( void ) { B* b = new D; de...

difference b/w destructor and finalise method

I want to know the sequence of how these function are called. Like if our heap is full, GC will be called. It will mark the object and call its finalise operation, now we have sweep stage.. in which the reference to that object is deleted and object becomes inaccessible.. So where does the destruction come in cycle...When wld it be calle...

Writing a LinkedList destructor?

Is this a valid LinkedList destructor? I'm still sort of confused by them. I want to make sure I'm understanding this correctly. LinkedList::~LinkedList() { ListNode *ptr; for (ptr = head; head; ptr = head) { head = head->next delete ptr; } } So at the beginning of the loop, pointer ptr is set to hold the add...

If changing a const object is undefined behavior then how do constructors and destructors operate with write access?

C++ standard says that modifying an object originally declared const is undefined behavior. But then how do constructors and destructors operate? class Class { public: Class() { Change(); } ~Class() { Change(); } void Change() { data = 0; } private: int data; }; //later: const Class object; //object.Change(); - won't co...

static destructor

Suppose I have: void foo() { static Bar bar; } Does c++ guarantee me that Bar::Bar() is called on bar, and Bar::~Bar() is never called on bar? (Until after main exits). Thanks! ...

WPF window class destruction

I create a window like this: if (someCondition) { MyWindow wnd = new MyWindow(); wnd.Owner = this; wnd.ShowDialog(); } I want MyWindow's destructor to be called at the closing curly bracket, but it doesn't. Do I need to call something like delete/destroy for MyWindow's destructor to be called? ...

Garbage collection of object after exception

I have observed that after an exception I have an object for which constructor is not called, which causes a lock to be held. What is the best way to improve the situation? Would calling del in an except block be the solution? b=BigHash(DB_DIR, url) meta = bdecode(b.get()) return meta b holds a lock which is released on destruction (i...

Why do un-named C++ objects destruct before the scope block ends?

The following code prints one,two,three. Is that desired and true for all C++ compilers? class Foo { const char* m_name; public: Foo(const char* name) : m_name(name) {} ~Foo() { printf("%s\n", m_name); } }; void main() { Foo foo("three"); Foo("one"); // un-named object printf("two\n"); } ...

Are destructors not meant to be called when returning that object (not as a pointer)?

I have a function: static Bwah boo(){ Bwah bwah; return bwah; } And a main function: int main(){ Bwah boo = Assigner::boo(); cout << "got here.." << endl; } The destructor to Bwah is only called once, after the "got here" print. Is this guaranteed or is this a compiler optimization? ...

Visual Studio Release and Debug builds destructor

In Visual Studio 2008 (C++) I have a class destructor (Let's call this class 'A') which deletes a pointer to a different class (Let's call this class 'B'). it looks like this: A::~A() { delete B; B = NULL; } My B class has a pointer to the instance of A that created it. In B's destructor, I delete everything in B, except for ...

using std::string with Qt causes run-time error on destruction

Hi. I have a Qt app that uses another library where the function output is std::string instead of a QString. So in my program I have a method void doSomething() { ... std::string std_string = MyExternalLibraryThatReturnsSTLstring.getString(); QString myQString = QString::fromStdString(std_string); ... process(myQString); ... } When ...

failed constructor and failed destructor in C++

Hi I have one question about failed constructor and failed destructor in C++. I noticed that when the constructor failed, an exception will be thrown. But there is no exception thrown in destructor. My question is 1) If constructor failed, what exception will be thrown? bad_alloc? or anything else related? Under what situation, a c...

Deleting an std::map (Visual C++)

Hi, I have a pointer to a map that I am trying to delete (this map was allocated with new). This map is valid I think, when I hover on it while debugging, it shows pMap: [0]() .. When I try to delete this empty map, my app just quits and I get a First-chance exception at 0xsomelocation in myapp.exe: 0xsomenumber: The object invo...