destructor

Trivial Destructibility and Necessity of Calling Destructor

独自空忆成欢 提交于 2019-11-29 07:40:33
Suppose there exists a type T such that std::is_trivially_destructable<T>::value == true , and suppose further that T is the value type of some vector class. When the vector's destructor is called, or the vector is assigned to another vector, it must destroy and deallocate its current storage. As T is trivially destructable, is it necessary that I call T 's destructor? Thanks for your help! According to the C++ Standard (section 3.8), you can end an object's lifetime by deallocating or reusing its storage. There's no need to call a destructor that does nothing. On the other hand, letting the

Aren't destructors guaranteed to finish running?

拟墨画扇 提交于 2019-11-29 07:07:51
Destructors are weird . I was attempting to eliminate the need of using the disposable pattern by using 'smart' reference management, ensuring that the garbage collector could collect objects at the correct time. In one of my destructors I had to wait for an event from another object, which I noticed it didn't. The application simply shut down and the destructor was terminated in middle of execution. I'd expect a destructor is always allowed to finish running, but as the following test indicates that is not true. using System; using System.Diagnostics; using System.Threading; namespace

Is relying on __del__() for cleanup in python unreliable?

好久不见. 提交于 2019-11-29 06:52:55
I was reading about different ways to clean up objects in Python, and I have stumbled upon these questions ( 1 , 2 ) which basically say that cleaning up using __del__() is unreliable and the following code should be avoid: def __init__(self): rc.open() def __del__(self): rc.close() The problem is, I'm using exactly this code, and I can't reproduce any of the issues cited in the questions above. As far as my knowledge goes, I can't go for the alternative with with statement, since I provide a Python module for a closed-source software (testIDEA, anyone?) This software will create instances of

Cocos2dx memory management, how to use destructors and when to release objects?

孤街醉人 提交于 2019-11-29 06:06:43
I'm reading around the web and the documentation but to be honest, I don't get it. Since I'm new to cocos2d-x I would like to understand better how the objects are created/retained and what I'm supposed to do to release them (if required). The thing that confuses me is the usage of smart pointers that I don't know very well. Imagine that in my CCLayer (added to the CCScene) I add a CCSprite, so i do: this->sprite = CCSprite::create("mySprite.png"); this->addChild(sprite); then since I've used create() I'm supposed to release it somewhere? in the destructor of the CCLayer maybe? or I have

Weird behaviour of C++ destructors

我与影子孤独终老i 提交于 2019-11-29 04:52:41
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000, vector<int>(4, -1)); cout << dp.size(); } This tiny program takes a split second to execute when simply run from the command line. But when run in a debugger, it takes over 8 seconds. Pausing the debugger reveals that it is in the middle of destroying all those vectors. WTF? Note - Visual Studio 2008 SP1, Core 2 Duo 6700 CPU with 2GB of RAM. Added: To clarify, no, I'm not confusing Debug and Release builds. These results are on one and the same .exe, without even any recompiling inbetween.

When should I provide a destructor for my class?

元气小坏坏 提交于 2019-11-29 02:57:23
问题 This seems like a rather trivial or at least common question, but I couldn't find a satisfying answer on google or on SO. I'm not sure when I should implement a destructor for my class. An obvious case is when the class wraps a connection to a file, and I want to make sure the connection is closed so I close it in the destructor. But I want to know in general, how can I know if I should define a destructor. What guidelines are there that I can check to see if I should have a destructor in

C++ Destructors with Vectors, Pointers,

我只是一个虾纸丫 提交于 2019-11-29 02:51:43
As far as I know, I should destroy in destructors everything I created with new and close opened filestreams and other streams. However, I have some doubts about other objects in C++: std::vector and std::string s: Are they destroyed automatically? If I have something like std::vector<myClass*> of pointers to classes. What happens when the vector destructor is called? Would it call automatically the destructor of myClass ? Or only the vector is destroyed but all the Objects it contains are still existant in the memory? What happens if I have a pointer to another class inside a class, say:

Are signals in Qt automatically disconnected when one of the class is deleted

荒凉一梦 提交于 2019-11-29 02:48:27
Does Qt automatically remove connections between objects , when one of the side is deleted ? e.g connect (A .. , B ..) , when A (a pointer) is deleted , or B is deleted , will the connection be disconnected ? Is it necessary to use disconnect explicitly in destructor ? Yes, the QObject::~QObject destructor takes care of that: All signals to and from the object are automatically disconnected, and any pending posted events for the object are removed from the event queue. However, it is often safer to use deleteLater() rather than deleting a QObject subclass directly. Do take care though: Warning

Is it legal to call member functions after an object has been explicitly destroyed but before its memory was deallocated?

南笙酒味 提交于 2019-11-29 02:31:44
问题 I have this code: struct data { void doNothing() {} }; int main() { data* ptr = new data(); ptr->~data(); ptr->doNothing(); ::operator delete(ptr); } Note that doNothing() is being called after the object has been destroyed but before its memory was deallocated. It looks like "object lifetime" has ended however the pointer still points to proper allocated memory. The member function does not access any member variables. Would member function call be legal in this case? 回答1: Yes, in the case

PHP: destructor vs register_shutdown_function

倾然丶 夕夏残阳落幕 提交于 2019-11-29 01:42:22
I have a PHP class that creates a PNG image on the fly and sends it to browser. PHP manual says that I need to make sure that imagedestroy function is called at end to release the memory. Now, if I weren't using a class, I would have some code like this: function shutdown_func() { global $img; if ($img) imagedestroy($img); } register_shutdown_function("shutdown_func"); However, I believe that appropriate place for my class would be to place a call to imagedestroy in class' destructor. I failed to find out if destructors get called the same way shutdown functions does? For example, if execution