destructor

Destructor of a static object constructed within the destructor of another static object

ぐ巨炮叔叔 提交于 2019-12-05 04:07:36
I have some problems with destructor, in next code: #include <stdlib.h> #include <cstdio> class Foo2 { public: Foo2() { printf("foo2 const\n"); } ~Foo2() { printf("foo2 dest\n"); // <--- wasn't called for bionic libc } }; static Foo2& GetFoo2() { static Foo2 foo2; printf ("return foo2\n"); return foo2; } class Foo1 { public: Foo1() { printf("foo1 const\n"); } ~Foo1() { printf("foo1 dest\n"); GetFoo2(); } }; int main( int argc, const char* argv[] ) { printf("main 1 \n"); static Foo1 anotherFoo; printf("main 2 \n"); } Why destructor for foo2 wasn't called for bionic and was for glibc ? EDIT

When is destructor called in a WCF service

為{幸葍}努か 提交于 2019-12-05 03:31:06
I need to create a service which will maintain a WCF session. In the constructor I read in data from the DB and when the session ends I have to save it back. If I understand correctly the session ends when I call Close() on the Client (My client ServiceClient was created with SvcUtil.exe). When I test it I see that it is sometimes called after approx. 10 minutes, sometimes after 20 minutes and sometimes not at all. So when is the destructor called? Service [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class Service:IService { private User m_User = null; public

Is it safe to delete a POD object by a pointer to its base?

▼魔方 西西 提交于 2019-12-05 02:11:43
Actually I am thinking about trivially destructible objects, not only about POD (I am not sure POD can have base class). When I read this explanation for is_trivially_destructible from cppreference I notice this: Storage occupied by trivially destructible objects may be reused without calling the destructor. So, it is safe to do that: struct A { int a; }; struct B : A { int b; }; int main() { A* a = new B; delete a; } B::~B() won't be called - and AFAIK (please correct if I am wrong) the entire memory will be freed. And B::~B() for sure is trivial. I know this code smells badly, but my

Why is QObject destroyed signal called AFTER the destruction?

我们两清 提交于 2019-12-05 02:04:12
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()), &tracker, SLOT(onDestructor())); delete obj; return app.exec(); } It prints this: MyObject constructor

C++ stack allocated object, explicit destructor call

五迷三道 提交于 2019-12-04 22:24:53
问题 I came across a strange use of the destructor while working on an existing library. The destructor of a stack allocated stl vector was being called explicitly, when its the case that that object may need to be used again. These vector objects are a slightly customised version of the stl vector class that have a specialized clear method. In the destructor body there exist two method calls: clear() , _Tidy() . I've been trying to think of a good reason why this destructor is being called rather

Destructor for a linked List

感情迁移 提交于 2019-12-04 20:41:35
I have a linked_list and currently my destructor is not working properly. Not entirely sure why. Can somebody explain me how to solve this? class linked_list { private: struct node { // String in this node std::string data; // Pointer to next node struct node *next; }; //First item in the list struct node *first; Here is my destructor linked_list::~linked_list(void) { while (first) { delete first; first = first->next; } } The problem lies here: delete first; first = first->next; When you delete first , but then try to access first->next . Cache first->next into a temp variable of type node* ,

Concerning Struct Constructor and Destructor behavior - C++

天涯浪子 提交于 2019-12-04 20:34:41
问题 I don't understand why the output of this program is as follows. Why isn't there a compilation error? I thought when trying to construct B, the compiler would find no function called foo() and report an error. #include <iostream> using namespace std; struct A{ int a; A(int i=0) : a(i) { cout << "A" << endl; } ~A() { cout << "Bye A" << endl; } int foo() { return a; } }; struct B{ int b; B(int i=0) : b(i) { cout << "B" << endl; } ~B() { cout << "Bye B" << endl; } int bar() { return b; } };

how much does the default destructor do

蓝咒 提交于 2019-12-04 18:33:40
问题 Does the default destructor in C++ classes automatically delete members that are not explicitly allocated in code? For example: class C { public: C() {} int arr[100]; }; int main(void) { C* myC = new C(); delete myC; return 0; } Does delete myC deallocate myC's arr automatically? Or do I need to write C's destructor to do this explicitly? 回答1: The constructor (in the absence of any ctor-initializer-list ) calls the default constructor for each subobject. Since you have no base classes and

Destructor that calls a function that can throw exception in C++

北城以北 提交于 2019-12-04 18:31:59
问题 I know that I shouldn't throw exceptions from a destructor. If my destructor calls a function that can throw an exception, is it OK if I catch it in the destructor and don't throw it further? Or can it cause abort anyway and I shouldn't call such functions from a destructor at all? 回答1: Yes, that's legal. An exception must not escape from the destructor, but whatever happens inside the destructor, or in functions it calls, is up to you. (Technically, an exception can escape from a destructor

How to delete an object of a polymorphic class type that has no virtual destructor

跟風遠走 提交于 2019-12-04 18:16:36
问题 I am getting the following error when I try to compile some code from a Third-party SDK. *Description Resource Path Location Type deleting object of polymorphic class type ‘Vendor_sys::VendorCode’ which has non-virtual destructor might cause undefined behaviour [-Werror=delete-non-virtual-dtor] PnServer.cpp /PCounter line 467 C/C++ Problem* I do not know if its possible to satisfy this condition with only partial knowledge of the Vendor's SDK, where most of the heavy lifting is done in a dll