destructor

Using exit() in destructor of a class having static object, doesn't end up in an infinite loop as expected

陌路散爱 提交于 2019-12-11 23:43:41
问题 I came across this in chapter 10, Thinking in C++ vol.1 by Bruce Eckel. Destructors for static objects (that is, all objects with static storage, not just local static objects as in the example above) are called when main( ) exits or when the Standard C library function exit( ) is explicitly called. In most implementations, main( ) just calls exit( ) when it terminates. This means that it can be dangerous to call exit( ) inside a destructor because you can end up with infinite recursion I

Prevent destructor from being called manually

你。 提交于 2019-12-11 23:24:54
问题 I have this class: class Test { private $test = 'ok'; public function doTest() { echo $this->test; } public function __destruct() { $this->test = 'not ok'; } } and the following test case: $test = new Test; $test->__destruct(); // I wish this would throw a Fatal Error or something... $test->doTest(); // prints "not ok" What I want to accomplish is to prevent __destruct() from being called manually, so that doTest() will never print "not ok". I tried setting the destructor's visibility to

C++ Object Composition, Depdendency Injection and Copy Constructors

被刻印的时光 ゝ 提交于 2019-12-11 19:43:24
问题 I want to design a class in C++ called Entity. That entity has a pointer to a member of Vector3D which is the position of the entity in 3D space. The constructor allows a pointer of type Vector3D to be passed to the constructor, such that the Vector3D instance is instantiated outside of the class. Because there is a pointer to a dynamically allocated object, the copy constructor and assignment operator must be overloaded to deep copy the vector. However, because the vector can be passed in

Is destructor called at the end of main(); strange behavior

南楼画角 提交于 2019-12-11 16:46:19
问题 // Foo.h class Foo { public: Foo(); ~Foo(); void exec(); }; // Foo.cpp Foo::~Foo() { // Statements A exit(0); } //main.cpp int main() { Foo foo; foo.exec(); // Statements B return 0; } So why both Statements A and B get executed? And is the destructor called when we reach return 0? I have seen variants where void exec(); is int exec(); and the main function ends with return foo.exec(); does this calls the destructor? Because I want to have an object that takes control of the code flow from

delete[] crashes program

↘锁芯ラ 提交于 2019-12-11 16:07:02
问题 Here is the code. What interests me is new_data. Why delete[] new_data crashes program? How to fix it? There is a lot going on inside functions and anyway I am writing it to post this post as I am getting: "It looks like your post is mostly code; please add some more details." error, so sorry for the spam but I have such problem with a lot of lines of code so SO asks me to describe my problem with many words. char * new_data; ... new_data = new char[end_file-beg_file+20000000]; string i_str;

Custom destructor in Python

扶醉桌前 提交于 2019-12-11 15:45:34
问题 Let's say I have two classes: class Container(): def __init__(self, name): self.name = name class Data(): def __init__(self): self._containers = [] def add_container(self,name): self._containers.append(name) setattr(self, name, Container(name)) Now let's say myData = Data() myData.add_container('contA') Now, if I do del myData.contA it of course doesn't remove name from myData._containers . So how would I write a destructor in Container so it deletes the attribute but also removes name from

using an object after it's destructor is called [duplicate]

纵饮孤独 提交于 2019-12-11 13:24:12
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Can a local variable’s memory be accessed outside its scope? Code: #include <iostream> using namespace std; class B{ public: int b; B():b(1){} ~B(){cout << "Destructor ~B() " << endl;} }; class A{ public: B ob; A()try{throw 4;} catch(...){cout << "Catched in A() handler : ob.b= " << ob.b<<endl;} }; int main()try{ A t; } catch(...){cout << "CATCHED in Main" << endl;} Output: Destructor ~B() Catched in A() handler

Destruction of object in local scope

为君一笑 提交于 2019-12-11 12:46:15
问题 Suppose that I have the following code: void foo() { { myclass object; object.do_something(); } cout<<"hello"<<endl; } Is ~myclass() guaranteed to be called by the time the local scope is exited , or might it be called at a later time (such as when the function returns)? 回答1: Yes, it is guaranteed: [class.dtor] Destructors are invoked implicitly for ... a constructed object with automatic storage duration when the block in which the object is created exits. 来源: https://stackoverflow.com

Avoiding memory leaks while using vector<Mat>

二次信任 提交于 2019-12-11 11:55:28
问题 I am trying to write a code that uses opencv Mat objects it goes something like this Mat img; vector<Mat> images; for (i = 1; i < 5; i++) { img.create(h,w,type) // h,w and type are given correctly // input an image from somewhere to img correctly. images.push_back(img); img.release() } for (i = 1; i < 5; i++) images[i].release(); I however still seem to have memory leakage can anyone tell me why it is so? I thought that if the refcount of a mat object = 0 then the memory should be

Destructor not working in C++ for anonymous object?

喜你入骨 提交于 2019-12-11 11:43:20
问题 My friend told me C++ allows us to call a member function even if the instance is destroyed from memory. So I write the code below to verify it, but why the value of a can be extracted even after the object was destroyed? I thought there would be a segment fault. #include <iostream> class Foo{ public: Foo(int a = 0){ std::cout << "created" << std::endl; this->a = a; } ~Foo(){ std::cout << "destroyed" << std::endl; } Foo *f(){ std::cout << "a=" << a << std::endl; return this; } private: int a;