destructor

Undefined behaviour with non-virtual destructors - is it a real-world issue?

别等时光非礼了梦想. 提交于 2019-12-10 17:32:49
问题 Consider the following code: class A { public: A() {} ~A() {} }; class B: public A { B() {} ~B() {} }; A* b = new B; delete b; // undefined behaviour My understanding is that the C++ standard says that deleting b is undefined behaviour - ie, anything could happen. But, in the real world, my experience is that ~A() is always invoked, and the memory is correctly freed. if B introduces any class members with their own destructors, they won't get invoked, but I'm only interested in the simple

C++ destruction order: Calling a field destructor before the class destructor

不羁的心 提交于 2019-12-10 16:47:46
问题 Is there any way to call a field destructor before the class destructor? Suppose I have 2 classes Small and Big , and Big contains an instance of Small as its field as such: class Small { public: ~Small() {std::cout << "Small destructor" << std::endl;} }; class Big { public: ~Big() {std::cout << "Big destructor" << std::endl;} private: Small small; }; int main() { Big big; } This, of course, calls the big destructor before the small destructor: Big destructor Small destructor I need the Small

C# Destructor not working as expected

百般思念 提交于 2019-12-10 13:57:55
问题 Please see the code below. I expect it to print either 10 because I have explicitly invoked the garbage collector. But I always get either a 0 or 20 as output. Why is that? void Main() { Panda[] forest_panda = new Panda[10]; for(int i=0; i<forest_panda.GetLength(0);i++) { forest_panda[i]=new Panda("P1"); } for(int i=0; i<forest_panda.GetLength(0);i++) { forest_panda[i]=new Panda("P1"); } System.GC.Collect(); Console.WriteLine("Total Pandas created is {0}",Panda.population); } class Panda {

Why is the dtor being called (using annoymous/lambda func)

北慕城南 提交于 2019-12-10 13:50:39
问题 I am trying to mimic a finally like effect. So i thought i should run a quick dirty test. The idea was to use Most Important const to stop destruction and to put the finally block in a lambda. However apparently i did something wrong and its being called at the end of MyFinally(). How do i solve this problem? #include <cassert> template<typename T> class D{ T fn; public: D(T v):fn(v){} ~D(){fn();} }; template<typename T> const D<T>& MyFinally(T t) { return D<T>(t); } int d; class A{ int a;

Where to free dynamically allocated TFrame's components' objects?

喜夏-厌秋 提交于 2019-12-10 13:29:09
问题 I have a form containing a TFrame . The TFrame contains a ComboBox that is dynamically populated. Each ComboBox entry has an associated object. By the time the overridden destructor for the TFrame is called, the Items in the ComboBox have already been cleared without freeing their associated objects. This happens whether I drop the ComboBox on the form in designer view, or dynamically create it in code with either nil or the TFrame as its owner. I currently use the OnDestroy event of the

The state of an object between a call to ~Derived() and ~Base()

跟風遠走 提交于 2019-12-10 13:12:20
问题 Question What does the C++ standard guarantee about the state of an object in the time after a derived class's destructor executes, but before the base class's destructor executes ? (This is the time when the derived class's subobjects' destructors are being called.) Example #include <string> struct Base; struct Member { Member(Base *b); ~Member(); Base *b_; }; struct Base { virtual void f() {} virtual ~Base() {} }; struct Derived : Base { Derived() : m(this) {} virtual ~Derived() {} virtual

C++ Static objects in DLLs not having destructors called

戏子无情 提交于 2019-12-10 11:24:21
问题 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

Non-virtual trivial destructor + Inheritance

依然范特西╮ 提交于 2019-12-10 10:29:35
问题 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();

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

…衆ロ難τιáo~ 提交于 2019-12-10 04:15:08
问题 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? 回答1: The equivalent to a C++ destructor is IDisposable

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

谁都会走 提交于 2019-12-10 03:38:30
问题 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