destructor

Can I call delete on primitives?

徘徊边缘 提交于 2019-12-05 08:44:54
I have a templated class, myFoo, which stores "stuff" of type T which can be either primitive or pointers to complex types. When myFoo is deleted, I want to release all the memory associated with everything it happens to be storing. This means I need to call delete on every pointer being stored but I might also end up calling delete on a primitive. Is this safe?? I've included a sketch of myFoo below to better highlight what's going on. I'm not sure if the behaviour of the destructor is well defined. template<class T> class myFoo { public: myFoo(int size) { size_ = size; T* foo = new T[size_];

Why do I always get “terminate called after throwing an instance of…” when throwing in my destructor?

心已入冬 提交于 2019-12-05 08:19:49
I'm trying to write a unit test that detects an invalid use of the lock() feature of my class. In order to do so, I want to use the destructor and throw an exception from there. Unfortunately, instead of catching the exception, g++ decides to call std::terminate(). There is a very simplified version of the class: class A { public: A() : f_lock(0) {} ~A() { if(f_lock) throw my_exception("still locked"); } lock() { ++f_lock; } unlock() { --f_lock; } private: int f_lock; }; There is a valid test: A *a = new A; a->lock(); ... a->unlock(); delete a; There is the invalid test I'm trying to write: A

Explicitly invoking `int` destructor - why is a type alias required? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 06:40:23
This question already has an answer here: Pseudo-destructor call does not destroy an object 1 answer The following program... int main() { int{1}.~int(); } does not compile on (see conformance viewer ) : clang++ trunk, with -std=c++1z g++ trunk, with -std=c++1z CL 19 2017 Introducing a type alias for int ... int main() { using X = int; int{1}.~X(); } ...makes the program valid on all previously mentioned compilers, without warnings (see conformance viewer ) . Why is a type alias required when invoking int 's destructor? Is this because int is not a valid grammar element for a destruction

Python destructor basing on try/finally + yield?

我与影子孤独终老i 提交于 2019-12-05 05:53:19
问题 I've been testing a dirty hack inspired by this http://docs.python.org/2/library/contextlib.html . The main idea is to bring try/finally idea onto class level and get reliable and simple class destructor. class Foo(): def __init__(self): self.__res_mgr__ = self.__acquire_resources__() self.__res_mgr__.next() def __acquire_resources__(self): try: # Acquire some resources here print "Initialize" self.f = 1 yield finally: # Release the resources here print "Releasing Resources" self.f = 0 f =

Non virtual destructor in base class, but virtual destructor in derived class cause segmentation fault

倾然丶 夕夏残阳落幕 提交于 2019-12-05 05:45:15
recently in a job interview I was asked about the problem of leaking memory in derived classes when the base class's destructor is not declared virtual. I wrote a small test to confirm my answer, but I found something interesting. Obviously, if you create a Derived object via new but store its pointer as a Base* , the derived object's destructor won't be called, if the pointer is deleted (so much for my answer to the question). I thought whether the derived class's destructor is virtual or not is irelevant in that case, but on my system the following code shows otherwise: #include <iostream>

extraneous calls to copy-constructor and destructor

蹲街弑〆低调 提交于 2019-12-05 05:28:41
问题 [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 more } } The output is: A Construction // 1 A Copy Construction // 1 A Destruction // 1 A Construction // 2 A Copy Construction // 2 A Copy Construction // WHY THIS? A Destruction // 2 A Destruction // deleting element from t A Destruction //

Destructor vs member function race

假如想象 提交于 2019-12-05 05:12:59
When I'm inside a destructor is it possible that some other thread will start executing object's member function? How to deal with this situation? C++ has no intrinsic protection against using an object after it's been deleting - forget about race conditions - another thread could use your object after it's been completely deleted. Either: Make sure only one place in the code owns the object, and it's responsible for deleting when no-one is using the object. Make the object reference counted - by added explicit reference counting code, or finding an appropriate base-class that implements

When is destructor called for C# classes in ASP.NET?

▼魔方 西西 提交于 2019-12-05 04:39:18
Say, I have my own C# class defined as such: public class MyClass { public MyClass() { //Do the work } ~MyClass() { //Destructor } } And then I create an instance of my class from an ASP.NET project, as such: if(true) { MyClass c = new MyClass(); //Do some work with 'c' //Shouldn't destructor for 'c' be called here? } //Continue on I'd expect the destructor to be called at the end of the if scope but it never is called. What am I missing? The equivalent to a C++ destructor is IDisposable and the Dispose() method, often used in a using block. See http://msdn.microsoft.com/en-us/library/system

Opening a database connection in a constructor, when should I close it?

前提是你 提交于 2019-12-05 04:35:39
问题 well, I've been thinking of making database requests a little faster by keeping the connection to the database open as long as the object is being used. So I was thinking of opening the connection in the constructor of that class. Now the question is, how can I close the connection after I stopped using? I have to call close() somewhere, don't I? I've been reading about the finalize() method, but people seemed to be skeptical about usage of this method anywhere at all. I'd expect it to have

Undefined behaviour on reinitializing object via placement new on this pointer

巧了我就是萌 提交于 2019-12-05 04:31:24
I saw a presentation on cppcon of Piotr Padlewski saying that the following is undefined behaviour: int test(Base* a){ int sum = 0; sum += a->foo(); sum += a->foo(); return sum; } int Base::foo(){ new (this) Derived; return 1; } Note: Assume sizeof(Base) == sizeof(Derived) and foo is virtual. Obviously this is bad, but I'm interested in WHY it is UB. I do understand the UB on accessing a realloc ed pointer but he says, that this is the same. Related questions: Is `new (this) MyClass();` undefined behaviour after directly calling the destructor? where it says "ok if no exceptions" Is it valid