destructor

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

萝らか妹 提交于 2019-11-30 08:07:45
问题 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

Destruction of return value on destructor exception

岁酱吖の 提交于 2019-11-30 07:50:54
I have the following code: #include <stdexcept> #include <iostream> struct ok { int _n; ok(int n) : _n(n) { std::cerr << "OK" << n << " born" << std::endl; } ~ok() { std::cerr << "OK" << _n << " gone" << std::endl; } }; struct problematic { ~problematic() noexcept(false) { throw std::logic_error("d-tor exception"); } }; ok boo() { ok ok1{1}; problematic p; ok ok2{2}; return ok{3}; // Only constructor is called... } int main(int argc, char **argv) { try {boo();} catch(...) {} } I see that he destructor of ok{3} is not called, the output is: OK1 born OK2 born OK3 born OK2 gone OK1 gone Is it the

What is the difference between delete and calling destructor in C++

老子叫甜甜 提交于 2019-11-30 07:39:49
As stated in the title, here is my code: class Foo { public: Foo (int charSize) { str = new char[charSize]; } ~Foo () { delete[] str; } private: char * str; }; For this class what would be the difference between: int main () { Foo* foo = new Foo(10); delete foo; return 0; } and int main () { Foo* foo = new Foo(10); foo->~Foo(); return 0; } Calling a destructor releases the resources owned by the object, but it does not release the memory allocated to the object itself. The second code snippet has a memory leak. Whenever a call to destructor is made , the allocated memory to the object is not

C++:When creating a new objects inside a function and returning it as result, must I use the new operator to create the object?

ぐ巨炮叔叔 提交于 2019-11-30 07:25:16
I have two dummy questions which have confused me for a while. I did do some searching online and read through much c++ tutorials, however I cannot find concrete answers. Say we have a class named Node which is a building block of a singly linked list. class Node { int data; Node* next; } Fact1: local variables(non static) will be destroyed upon the exit of the corresponding functions. Question1: How about the situations blow: Node* func() { Node n; Node* ptr=&n; return n; } Will the node n be destroyed? Or we have to use the new operator to create the node and return a pointer to a heap

C++ destructor issue with std::vector of class objects

纵然是瞬间 提交于 2019-11-30 06:59:55
问题 I am confused about how to use destructors when I have a std::vector of my class. So if I create a simple class as follows: class Test { private: int *big; public: Test () { big = new int[10000]; } ~Test () { delete [] big; } }; Then in my main function I do the following: Test tObj = Test(); vector<Test> tVec; tVec.push_back(tObj); I get a runtime crash in the destructor of Test when I go out of scope. Why is this and how can I safely free my memory? 回答1: Your problem is here: Test tObj =

When should I provide a destructor for my class?

断了今生、忘了曾经 提交于 2019-11-30 05:13:49
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 this class? One such guideline I can think of, is if the class contains any member pointers. The default

why is a scalar deleting destructor being called as a result of vector delete on Windows?

别说谁变了你拦得住时间么 提交于 2019-11-30 05:09:49
问题 I have a code that is leaking on Windows. It runs fine on many unix platforms and the leak only occurs on Windows. The binary consists of exe, 1 dll and 2 static libs. The exe links to both the dll and the static libs, while the static libs link with the dll as well. The leak occurs in the exe code when instead of calling to a vector deleting destructor, for some reason scalar deleting destructor is called. This results in only the first object in the array to be deleted while the rest of the

Is there destructor in typeScript

不羁的心 提交于 2019-11-30 00:05:56
问题 Is there destructor in TypeScript? If not, how can I delete an object? I tried destructor() and ~ClassName() but it didn't work. 回答1: JavaScript uses garbage collection to automatically delete objects when they are no longer referenced. There is no concept of destructors or finalizers. You can't observe when an object is deleted by the garbage collector, nor is it predictable. 回答2: You can actually class MyClass { constructor(input1, input2){ this.in1 = input1; this.in2 = input2; } } let

How do constructors and destructors work?

蹲街弑〆低调 提交于 2019-11-29 23:40:57
问题 I'm trying to understand this code: class Person: '''Represents a person ''' population = 0 def __init__(self,name): //some statements and population += 1 def __del__(self): //some statements and population -= 1 def sayHi(self): '''grettings from person''' print 'Hi My name is %s' % self.name def howMany(self): '''Prints the current population''' if Person.population == 1: print 'i am the only one here' else: print 'There are still %d guyz left ' % Person.population rohan = Person('Rohan')

Why base class destructor (virtual) is called when a derived class object is deleted?

南楼画角 提交于 2019-11-29 22:55:18
A difference between a destructor (of course also the constructor) and other member functions is that, if a regular member function has a body at the derived class, only the version at Derived class gets executed. Whereas in case of destructors, both derived as well as base class versions get executed? It will be great to know what exactly happens in case of destructor (maybe virtual) & constructor, that they are called for all its base classes even if the most derived class object is deleted. Thanks in advance! The Standard says After executing the body of the destructor and destroying any