destructor

Why is QObject destroyed signal called AFTER the destruction?

戏子无情 提交于 2019-12-06 19:31:34
问题 Consider this test case: class MyObject : public QObject { Q_OBJECT public: MyObject() { qDebug() << "MyObject constructor"; } virtual ~MyObject() { qDebug() << "MyObject destructor"; } }; class Tracker : public QObject { Q_OBJECT public: Tracker() {} public slots: void onDestructor() { qDebug() << "About to be destroyed!"; } }; int main(int argc, char** argv) { QCoreApplication app(argc, argv); Tracker tracker; MyObject *obj = new MyObject(); QObject::connect(obj, SIGNAL(destroyed()),

Destructors in C++

混江龙づ霸主 提交于 2019-12-06 18:58:36
问题 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? 回答1: The 'compiler' doesn't delete anything. It creates code that does things at runtime. When you write delete somePointer; the compiler, in essence, writes: if ( has_virtual_destructor( * somePointer ) ) { // virtual dispatch to a compiler-generated function dynamic_cast< true_dynamic_type

How to do “try/finally” in C++ when RAII is not possible?

南笙酒味 提交于 2019-12-06 12:56:56
I'm coming back to C++ from a heavy C# background and I've inherited some C++ codebase which I think might not have been in line with the best C++ practices. For example, I'm dealing with the following case (simplified): // resource class Resource { HANDLE _resource = NULL; // copying not allowed Resource(const Resource&); Resource& operator=(const Resource& other); public: Resource(std::string name) { _resource = ::GetResource(name); if (NULL == _resource) throw "Error"; } ~Resource() { if (_resource != NULL) { CloseHandle(_resource); _resource = NULL; }; } operator HANDLE() const { return

Non-virtual trivial destructor + Inheritance

白昼怎懂夜的黑 提交于 2019-12-06 11:59:40
Given that a class and all its subclasses need no more than the default destructor to release their resources if stored in a variable of the exact type (or pointer to the exact type), can a subclass leak memory if referenced by a base class pointer and then deleted by that pointer? Example: #include <memory> class A { }; class B : public A { public: B () : pInt(new int) {} auto_ptr<int> pInt; // this is what might leak... possibly more will though }; void will_this_leak () { A *pA = new B(); delete pA; } "Leak memory"? Why are you talking about leaking memory specifically? The code you posted

Generic binary tree node destructor issue

淺唱寂寞╮ 提交于 2019-12-06 09:25:38
I've been working on an assignment and now I'm stuck with buggy destructors. I have to create a generic binary tree with all the usual member functions and some special operators. There's also a restriction: everything must work iteratively so no nasty recursive hacks this time. There is obviously something very wrong with the destructor of BinTreeNode class because if I delete the node like this: BinTreeNode<int> * node = new BinTreeNode<int>(); delete node; I can still access its data: node->getData(); //should fail miserably so deletion has no effect but I have no usable idea how I should

C++ Static objects in DLLs not having destructors called

浪子不回头ぞ 提交于 2019-12-06 08:03:47
I am having an issue where static destructors in a DLL are not getting called. The constructor is being called, but the destructor is not. I have a class like this in my DLL struct DLLDestructorTest { int blah; DLLDestructorTest() { blah = 2; } ~DLLDestructorTest() { blah = 0; } }; DLLDestructorTest DLLDestructorTestObj; I can put breakpoints in the constructor and the destructor and I can verify that the constructor breakpoint is hit and the destructor breakpoint is not. If I put an analogous chunk of code in my main.cpp file, I can verify that both the constructor and destructor will be

Calling std::~basic_string() in gdb

耗尽温柔 提交于 2019-12-06 05:36:47
As per @EvanED in https://stackoverflow.com/a/11311786/890753 I created a gdb command newstr to create a new std::string and put it in a gdb convenience variable: define newstr set ($arg0)=(std::string*)malloc(sizeof(std::string)) call ($arg0)->basic_string() # 'assign' returns *this; casting return to void avoids printing of the struct. call (void)( ($arg0)->assign($arg1) ) end It works great: (gdb) newstr $foo "hello world" (gdb) p $foo->c_str() $57 = 0xb22e388 "hello world" I use newstr in other custom gdb commands, so for tidyness I also created delstr : define delstr call ($arg0)->~basic

Is the whole object freed with a non-virtual destructor and a Base class pointer?

梦想的初衷 提交于 2019-12-06 05:16:30
问题 If a Base class does not have a virtual destructor (in order to avoid the vtable entry for instance) and the Derived class has only basic attributes, does it free all the memory allocated by new, when the pointer of the Base class is deleted? I know the destructor of the Derived class will not be called but I am wondering if the memory allocated by the whole object will be freed? I assume also that calling delete on a Derived pointer will free the whole memory space. Also, if it does not free

How to catch exception from member destructor

ⅰ亾dé卋堺 提交于 2019-12-06 02:26:29
问题 I wonder whether (and how) it's possible to catch an exception thrown in a member destructor. Example: #include <exception> class A { public: ~A() { throw std::exception("I give up!"); } }; class B { A _a; public: ~B() { // How to catch exceptions from member destructors? } }; 回答1: Yes, you can catch such an exception, using the function-try-block: class B { A _a; public: ~B() try { // destructor body } catch (const std::exception& e) { // do (limited) stuff } }; However, you cannot really do

How to use __del__ in a reliable way?

断了今生、忘了曾经 提交于 2019-12-06 01:37:38
问题 I have learned that python does not guarantee that __del__ is called whenever an object is deleted. In other words, del x does not necessarily invoke its destructor x.__del__() . If I want to ensure proper object cleanup, I should use a context manager (in a with statement). I know it's stupid, but for a couple of reasons (please don't ask why) I am tied to a system with Python 2.4; therefore context managers are out of question (they were introduced in Python 2.5) So I need a an alternative