destructor

Enforcing required function call

Hi, I have a "Status" class in C#, used like this: Status MyFunction() { if(...) // something bad return new Status(false, "Something went wrong") else return new Status(true, "OK"); } You get the idea. All callers of MyFunction should check the returned Status: Status myStatus = MyFunction(); if ( ! myStatus.IsOK() ...

In C++ can constructor and destructor be inline functions?

VC++ makes functions which are implemented within the class declaration inline functions. If I declare a class Foo as follows, then are the CONSTRUCTOR and DESTRUCTOR inline functions? class Foo { int* p; public: Foo() { p = new char[0x00100000]; } ~Foo() { delete [] p; } }; { Foo f; (f); } ...

VB.NET - Should a Finalize method be added when implementing IDisposable?

In Visual Studio, when I type the line "Implements IDisposable", the IDE automatically adds: a disposedValue member variable a Sub Dispose() Implements IDisposable.Dispose a Sub Dispose(ByVal disposing As Boolean) The Dispose() should be left alone, and the clean up code should be put in Dispose(disposing). However the Dispose Final...

Is there a way to determine if an exception is occurring?

In a destructor, is there a way to determine if an exception is currently being processed? ...

throwing exceptions out of a destructor

Most people say never throw an exception out of a destructor - doing so results in undefined behavior. Stroustrup makes the point that "the vector destructor explicitly invokes the destructor for every element. This implies that if an element destructor throws, the vector destruction fails... There is really no good way to protect agains...

RAII vs. exceptions

The more we use RAII in C++, the more we find ourselves with destructors that do non-trivial deallocation. Now, deallocation (finalization, however you want to call it) can fail, in which case exceptions are really the only way to let anybody upstairs know of our deallocation problem. But then again, throwing-destructors are a bad idea b...

Is there a destructor for Java?

Is there a destructor for Java? I don't seem to be able to find any documentation on this. If there isn't, how can I achieve the same effect? Thank you in advance. EDIT: First of all I thank everyone for swift reply. I see a lot of very good answers there. To make my question more specific, I am writing an application that deals with d...

Why is there no RAII in .NET?

Being primarily a C++ developer the absence of RAII (Resource Acquisition Is Initialization) in Java and .NET has always bothered me. The fact that the onus of cleaning up is moved from the class writer to its consumer (by means of try finally or .NET's using construct) seems to be markedly inferior. I see why in Java there is no suppor...

Is the destructor called if the constructor throws an exception?

Looking for an answer for C# and C++. (in C#, replace 'destructor' with 'finalizer') ...

RAII in Ruby (Or, How to Manage Resources in Ruby)

I know it's by design that you can't control what happens when an object is destroyed. I am also aware of defining some class method as a finalizer. However is the ruby idiom for C++'s RAII (Resources are initialized in constructor, closed in destructor)? How do people manage resources used inside objects even when errors or exception...

Why destructor is not called on exception?

I expected A::~A() to be called in this program, but it isn't: #include <iostream> struct A { ~A() { std::cout << "~A()" << std::endl; } }; void f() { A a; throw "spam"; } int main() { f(); } However, if I change last line to int main() try { f(); } catch (...) { throw; } then A::~A() is called. I am compiling with "Micr...

PHP: destructor vs register_shutdown_function

I have a PHP class that creates a PNG image on the fly and sends it to browser. PHP manual says that I need to make sure that imagedestroy function is called at end to release the memory. Now, if I weren't using a class, I would have some code like this: function shutdown_func() { global $img; if ($img) imagedestroy($im...

How do I create a generic std::vector destructor?

Having a vector containing pointers to objects then using the clear function doesn't call the destructors for the objects in the vector. I made a function to do this manually but I don't know how to make this a generic function for any kind of objects that might be in the vector. void buttonVectorCleanup(vector<Button *> dVector){ B...

C++: dynamically allocating an array of objects?

This is kind of a beginners question, but I haven't done C++ in a long time, so here goes... I have a class that contains a dynamically allocated array, say class A { int* myArray; A() { myArray = 0; } A(int size) { myArray = new int[size]; } ~A() { // Note that as per MikeB's...

Static Finalizer

What is the right way to perform some static finallization? There is no static destructor. The AppDomain.DomainUnload event is not raised in the default domain. The AppDomain.ProcessExit event shares the total time of the three seconds (default settings) between all event handlers, so it's not really usable. ...

C++ class member functions that use dummy parameters

I know that you can use a dummy "int" parameter on operator++ and operator-- to override the postfix versions of those operators, but I vaguely recall something about a dummy parameter that you could declare on a destructor. Does anyone know anything about that, and if so, what that dummy parameter did? This was in my old Turbo C++ tuto...

Why should I declare a virtual destructor for an abstract class in C++?

I know it is a good practice to declare virtual destructors for base classes in C++, but is it always important to declare virtual destructors even for abstract classes that function as interfaces? Please provide some reasons and examples why. ...

Forcing something to be destructed last in C++

I am working on a C++ app which internally has some controller objects that are created and destroyed regularly (using new). It is necessary that these controllers register themselves with another object (let's call it controllerSupervisor), and unregister themselves when they are destructed. The problem I am now facing is happening whe...

Unit testing destructors?

Is there any good way to unit test destructors? Like say I have a class like this (contrived) example: class X { private: int *x; public: X() { x = new int; } ~X() { delete x; } int *getX() {return x;} const int *getX() const {return x;} }; Is there any good way to unit test th...

What is the difference between using IDisposable vs a destructor in C#?

When would I implement IDispose on a class as opposed to a destructor? I read this article, but I'm still missing the point. My assumption is that if I implement IDispose on an object, I can explicitly 'destruct' it as opposed to waiting for the garbage collector to do it. Is this correct? Does that mean I should always explicitly ...