destructor

Why does Java not have any destructor like C++?

Java has its own garbage collection implementation so it does not require any destructor like C++ . This makes Java developer lazy in implementing memory management. Still we can have destructor along with garbage collector where developer can free resources and which can save garbage collector's work. This might improves the performan...

Why is the destructor of the class called twice ?

Apologies if the question sounds silly, I was following experts in SO and trying some examples myself, and this is one of them. I did try the search option but didn't find an answer for this kind. class A { public: A(){cout<<"A Contruction"<<endl;} ~A(){cout<<"A destruction"<<endl;} }; int main() { vector<A> t;...

extraneous calls to copy-constructor and destructor

[a follow up to this question] class A { public: A() {cout<<"A Construction" <<endl;} A(A const& a){cout<<"A Copy Construction"<<endl;} ~A() {cout<<"A Destruction" <<endl;} }; int main() { { vector<A> t; t.push_back(A()); t.push_back(A()); // once mo...

Another C++ question, delete not working?

New to c++, and am having a problem with delete and destructor (I am sure i am making a stupid mistake here, but haven't been able to figure it out as of yet). When i step through into the destructor, and attepmt to call delete on a pointer, the message shows up "Cannot access memory at address some address." The relevant code is: /* ...

C++ Suppress Automatic Initialization and Destruction

How does one suppress the automatic initialization and destruction of a type? While it is wonderful that T buffer[100] automatically initializes all the elements of buffer, and destroys them when they fall out of scope, this is not the behavior I want. #include <iostream> static int created = 0, destroyed = 0; struct S {...

Will exit() or an exception prevent an end-of-scope destructor from being called?

Let's say I have the following code: struct mytype { ~mytype() { /* do something like call Mix_CloseAudio etc */ } }; int main() { mytype instant; init_stuff(); start(); return 0; } Is that destructor guaranteed to be called even if exit() is used from somewhere inside start() ? ...

How do virtual destructors work?

Few hours back I was fiddling with a Memory Leak issue and it turned out that I really got some basic stuff about virtual destructors wrong! Let me put explain my class design. class Base { virtual push_elements() {} }; class Derived:public Base { vector<int> x; public: void push_elements(){ for(int i=0;i <5;i++) ...

destruction of a variable or array in C#

I have a variable or array, which I no longer needed. How to destroy them? Sorry for noob-question. ...

C++ destructor issue with std::vector of class objects

I am confused about how to use destructors when I have a std::vector of my class. So if I create a simple class as follows: class Test { private: int *big; public: Test () { big = new int[10000]; } ~Test () { delete [] big; } }; Then in my main function I do the following: Test tObj = Test(); vector<Test> tVec; tVec...

C++ LNK2019 error with constructors and destructors in derived classes

I have two classes, one inherited from the other. When I compile, I get the following errors: Entity.obj : error LNK2019: unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)" (??0Base@Parsables@Utility@@QAE@XZ) referenced in function "public: __thiscall Utility::Parsables::Entity::Entity(void)" (??0Ent...

Changes to data inside class not being shown when accessed from outside class.

I have two classes, Car and Person. Car has as one of its members an instance of Person, driver. I want to move a car, while keeping track of its location, and also move the driver inside the car and get its location. However, while this works from inside the class (I have printed out the values as they are calculated), when I try to acc...

C++ Singleton Constructor and Destructor

Does it matter if the constructor/destructor implementation is provided in the header file or the source file? For example, which way is preferred and why? Way 1: class Singleton { public: ~Singleton() { } private: Singleton() { } }; Way 2: class Singleton { public: ~Singleton(); private: Singleton(); }; ...

Is there any reason I shouldn't do database calls from the destructor?

I want to create a sort of datamapper library, where you'd do something like this: $users = Users::getTable(); $users->add($newUser1); $users->add($newUser2); Now $users contains 2 user records, but they aren't yet persisted to the database. To be efficient, I'd like to flush them all at once. I would like to have a flush() method to ...

Why exactly is calling the destructor for the second time undefined behavior in C++?

As mentioned in this answer simply calling the destructor for the second time is already undefined behavior 12.4/14(3.8). For example: class Class { public: ~Class() {} }; // somewhere in code: { Class* object = new Class(); object->~Class(); delete object; // UB because at this point the destructor call is attempted ag...

calling constructor of the class in the destructor of the same class

Experts !! I know this question is one of the lousy one , but still I dared to open my mind , hoping I would learn from all. I was trying some examples as part of my routine and did this horrible thing, I called the constructor of the class from destructor of the same class. I don't really know if this is ever required in real programm...

Alternatives to persisting objects than using __destruct() in PHP

I usually use a classes destructor method __destruct() to persist objects to session or what have you. It is just very convinient, but I'm curious to if there are any other methods that are equally appealing. Do you know of such? The curiousity arose as I was to merge/utilize two frameworks that both made use of __destruct() for persist...

php destructor behaviour question

Hello, im trying to understand php constructor and destructor behaviour. Everything goes as expected with the constructor but i am having trouble getting the destructor to fire implicitly. Ive done all the reading on php.net and related sites, but i cant find an answer to this question. If i have a simple class, something like: class ...

Double Free inside of a destructor upon adding to a vector

Hey, I am working on a drum machine, and am having problems with vectors. Each Sequence has a list of samples, and the samples are ordered in a vector. However, when a sample is push_back on the vector, the sample's destructor is called, and results in a double free error. Here is the Sample creation code: class XSample { public: ...

Will the template argument's destructor to a templated class be called on deletion?

If you have a templated base class as in the following example: class A{ public: A(); virtual ~A(); }; template <class T> class B : public T { public: B(); virtual ~B(); }; typedef B<A> C; class D : public C { public: D(); virtual ~D(); }; When you delete an instance of D, will the destructor of A be ...

Class destructor memory handling in C++

What potential memory leaks won't an implicit destructor handle? I know that if you have anything stored on the heap it won't handle it, and if you have a connection to a file or a database, that needs to be handled manually. Is there anything else? What about, say, non-base data types like vectors? Also, in an explicit destructor, need...