destructor

Why isn't copy constructor being called like I expect here using map?

I am having problems using my custom class with a std::map. The class dynamically allocates memory for members, and I do not want to use pointer in the map because I want to ensure that the class takes care of deleting all allocated memory. But the problem I am having is after I add item to map, when that block of code goes out of scop...

When will __destruct not be called in PHP?

class MyDestructableClass { function __construct() { print "\nIn constructor\n"; $this->name = "MyDestructableClass"; } function __destruct() { print "\nDestroying " . $this->name . "\n"; } } $obj = new MyDestructableClass(); When the above script is in a complex environment,the __destruct won't get c...

Why doesn't C# allow access modifiers on destructors?

I was making a simple class for a small project and decided to just add a destructor for a quick impl instead of using IDisposable, and I came across a compiler error whenever there is a destructor with an access modifier on it. public class MyClass { public ~MyClass() { // clean resources } } I tried public, priva...

Why doesn't the C++ default destructor destroy my objects?

The C++ specification says the default destructor deletes all non-static members. Nevertheless, I can't manage to achieve that. I have this: class N { public: ~N() { std::cout << "Destroying object of type N"; } }; class M { public: M() { n = new N; } // ~M() { //this should happen by default // d...

Declaring refrences /objects in Destructor of a class

Does it make any sense of declaring objects or refrences in a destructor of a class in C++? I mean class A { A() { } ~A() { //Declaring refrences or objects here // } } ...

Cleaning up threads referencing an object when deleting the object (in C++)

I have an object (Client * client) which starts multiple threads to handle various tasks (such as processing incoming data). The threads are started like this: // Start the thread that will process incoming messages and stuff them into the appropriate queues. mReceiveMessageThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)receive...

Destruction of string temporaries in thrown exceptions

Consider the following code: std::string my_error_string = "Some error message"; // ... throw std::runtime_error(std::string("Error: ") + my_error_string); The string passed to runtime_error is a temporary returned by string's operator+. Suppose this exception is handled something like: catch (const std::runtime_error& e) { st...

C++ freeing static variables

Hi all, I would like my class to have a static pointer to a dynamically allocated region of memory. I understand how to initialize it - in my case I will initialize it when the first object needs it. However, I don't know when/where in the code to free it. I'd like to free it when the program terminates. I might be able to free the ...

Can someone here explain constructors and destructors in python - simple explanation required - new to programming

i will try to see if it makes sense :- class Person: '''Represents a person ''' population = 0 def __init__(self,name): //some statements and population += 1 def __del__(self): //some statements and population -= 1 def sayHi(self): '''grettings from person''' print 'Hi My name i...

g++ __static_initialization_and_destruction_0(int, int) - what is it

Hello After compiling of c++ file (with global static object) I get in nm output this function: 00000000 t _Z41__static_initialization_and_destruction_0ii __static_initialization_and_destruction_0(int, int) /* after c++filt */ What is it? It will call __cxa_atexit() Can I disable generation of this function (and calling a __cxa_...

An interesting case of delete and destructor (C++)

I have a piece of code where I can call destructor multiple times and access member functions even the destructor was called with member variables' values preserved. I was still able to access member functions after I called delete but the member variables were nullified (all to 0). And I can't double delete. Please kindly explain this. ...

Destructors in C++

Does the destructor deallocate memory assigned to the object which it belongs to or is it just called so that it can perform some last minute housekeeping before the object is deallocated by the compiler? ...

Destructors not called when native (C++) exception propagates to CLR component

We have a large body of native C++ code, compliled into DLLs. Then we have a couple of dlls containing C++/CLI proxy code to wrap the C++ interfaces. On top of that we have C# code calling into the C++/CLI wrappers. Standard stuff, so far. But we have a lot of cases where native C++ exceptions are allowed to propagate to the .Net wor...

C++: Life span of temporary arguments?

When creating a new instance of a MyClass as an argument to a function like so: class MyClass { MyClass(int a); }; myFunction(MyClass(42)); does the standard make any grantees on the timing of the destructor? Specifically, can I assume that the it is going to be called before the next statement after the call to myFunction() ? ...

Destructor - does it get called if the app crashes

Does a destructor get called if the app crashes? If it's an unhandled exception I'm guessing it does, but what about more serious errors, or something like a user killing the application process? And a few more potentially dumb questions: what happens to all the objects in an app when the app exits and all finalizers have been execut...

Why, really, deleting an incomplete type is undefined behaviour?

Hi, consider this classic example used to explain what not to do with forward declarations: //in Handle.h file class Body; class Handle { public: Handle(); ~Handle() {delete impl_;} //.... private: Body *impl_; }; //--------------------------------------- //in Handle.cpp file #include "Handle.h" class Bod...

Class Destructor Problem

I am making a simple class that contains a StreamWrite class Logger { private StreamWriter sw; private DateTime LastTime; public Logger(string filename) { LastTime = DateTime.Now; sw = new StreamWriter(filename); } public void Write(string s) { sw.WriteLine((DateTime.Now-LastTime...

Getting segmentation fault after destructor

I'm making a small file reading and data validation program as part of my TAFE (a tertiary college) course, This includes checking and validating dates. I decided that it would be best done with a seperate class, rather than integrating it into my main driver class. The problem is that I'm getting a segmentation fault(core dumped) afte...

How does the destructor know when to activate itself? Can it be relied upon?

Say for example i have the following code (pure example): class a { int * p; public: a() { p = new int; } ~a() { delete p; } }; a * returnnew() { a retval; return(&retval); } int main() { a * foo = returnnew(); return 0; } In returnnew(), would retval be destructed after the return of the funct...

How is destroying local variables when a block is exited normally called in C++?

C++ automagically calls destructors of all local variables in the block in reverse order regardless of whether the block is exited normally (control falls through) or an exception is thrown. Looks like the term stack unwinding only applies to the latter. How is the former process (the normal exit of the block) called concerning destroyi...