destructor

In C# what is the difference between a destructor and a Finalize method in a class?

A question for the C# gurus out there. What is the difference, if there is one, between a destructor and a Finalize method in a class? I recently discovered that Visual Studio 2008 considers a destructor synonymous with a Finalize method, meaning that Visual Studio won't let you simultaneously define both methods in a class. For examp...

How do you Specify a Method to be a Destructor Rather than a Constructor in C++?

How do you specify a method to be a destructor rather than a constructor in C++? This confuses me very much. I can't tell the difference between the two. ...

destructor problem

this is my addCard function which takes a playingCard as a parameter then hands over the address of its self to an allocated array of pointers to playingCard objects. void cardHand::addCard(playingCard card) { theHand[nElems++] = &card; } // addCard() now when i run my program it runs fine but then crashes when the destructor is c...

remoting and destructors in c#

Hi, I am playing with the .net remoting features and there is something that I can't figure out nor find an answer in Google and is how the object disposal works. I try to implement some kind of object pooling with remoting, for that I've list of static objects that are basicly a string and boolean state indicator. When I request fo...

How do I *not* delete a member in a destructor?

I'd like the destructor of my class to delete the entire object except for one of the members, which is deleted elsewhere. First of all, is this totally unreasonable? Assuming it's not, how do I do this? I thought that created an destructor with an empty body would prevent all the members from being deleted (because the destructor wou...

A proper way of destroying a TThread object

This question may seem trivial, but I hope you won't ignore it. Before destroying a TThread object it is usually necessary to wait until the thread that called the TThread.Execute() method finishes, for only then can we be sure that, for instance, the objects destroyed inside the class's destructor are no longer accessed. Therefore it is...

What is the destruction order of the View/Doc/Frame in a CMultiDocTemplate?

I'm working in an MDI application that has a pointer to a document's frame object. Other threads are calling PostMessage using the pointer. During shutdown, the threads continue trying to post messages to the frame while the frame is being destructed. Does anyone know the destruction order of the multiple documents in MFC's MDI implement...

How to correctly destruct token semantic values (symbol records) and avoid memory leaks using GNU Bison?

I'm writing a simplified Pascal parser/interpreter and now I'm thinking about segmentation faults. I'm not getting them yet, everything is running fine, but since I'm developing under Cygwin, I can't test the program through valgrind. Basically what I'm doing is described below: typedef struct{ char idType; //Integer (i), Real (r),...

delete a NULL pointer does not call overloaded delete when destructor is written

class Widget { public: Widget() { cout<<"~Widget()"<<endl; } ~Widget() { cout<<"~Widget()"<<endl; } void* operator new(size_t sz) throw(bad_alloc) { cout<<"operator new"<<endl; throw bad_alloc(); } void operator delete(void *v) { cout<<"oper...

Can the default destructor be generated as a virtual destructor automatically?

Can the default destructor be generated as a virtual destructor automatically? If I define a base class but no default destructor, is there a default virtual destructor generated automatically? ...

Finalization Reachable Table

If I implement a destructor in a class, Foo, instances of Foo are tracked closely on the finalization queue. When an instance of Foo is garbage collected, I understand that the CLR sees the entry in the finalization queue and gives that object special treatment by moving the object off the heap and into the finalization reachable table. ...

Is there any automated way to implement post-constructor and pre-destructor virtual method calls?

Hi all, Due to the well-known issues with calling virtual methods from inside constructors and destructors, I commonly end up with classes that need a final-setup method to be called just after their constructor, and a pre-teardown method to be called just before their destructor, like this: MyObject * obj = new MyObject; obj->Initiali...

Destructor that calls a function that can throw exception in C++

I know that I shouldn't throw exceptions from a destructor. If my destructor calls a function that can throw an exception, is it OK if I catch it in the destructor and don't throw it further? Or can it cause abort anyway and I shouldn't call such functions from a destructor at all? ...

Why do we need a pure virtual destructor in C++?

I understand the need for a virtual destructor. But why do we need a pure virtual destructor? In one of the C++ articles, the author has mentioned that we use pure virtual destructor when we want to make a class abstract. But we can make a class abstract by making any of the member functions as pure virtual. So my questions are Wh...

Can you guarantee destructor order when objects are declared on a stack?

I have code that controls a mutex lock/unlock based on scope: void PerformLogin() { ScopeLock < Lock > LoginLock( &m_LoginLock ); doLoginCommand(); ScopeLock < SharedMemoryBase > MemoryLock( &m_SharedMemory ); doStoreLogin(); ... } Can I guarantee that MemoryLock will be destructed before LoginLock? ...

Is it OK to use "delete this" to delete the current object?

I'm writing a linked list and I want a struct's destructor (a Node struct) to simply delete itself, and not have any side effects. I want my list's destructor to iteratively call the Node destructor on itself (storing the next node temporarily), like this: //my list class has first and last pointers //and my nodes each have a pointer to...

When is my destructor called in this circumstance? (C#)

I was wondering when the destructor is called under these circumstances, and if it is will it be called on the main UI thread? Let's say I have the following code, when would the destructor be called, and would it wait until I have finished all my function calls? private void Foo() { MyObject myObj = new MyObject(); DoSomeFunThin...

Error: Do not override object.Finalize. Instead, provide a destructor

Getting the above error in following code. How to rectify it. Thanks. Please look for protected override void Finalize() { Dispose(false); } in the below code. using Microsoft.Win32; using System.Runtime.InteropServices; public class Kiosk : IDisposable { #region "IDisposable" // Implementing IDisposable since it...

What is the mechanism through which destructors are called for stack-assigned objects?

How does C++ ensure that destructors are called for stack assigned objects? What happens to the destructor function (or a pointer to it) when I assign dynamic memory as follows: class MyClass { public: ~MyClass() { std::cout<<"Destructor called."<<std::endl; } MyClass() { std::cout<<"Constructor called."<<std::endl...

Clearing controls from FlowLayoutPanel not calling destructors?

Sorry if I'm missing something obvious, but I'm trying to clear the controls (a series of user controls) from a FlowLayoutPanel - (panelName).Controls.Clear();. Unfortunately this doesn't seem to be calling the destructors for the objects on the panel - the User Objects column in the task manager just keeps going up and up, until it hits...