destructor

In C# what is the difference between a destructor and a Finalize method in a class?

独自空忆成欢 提交于 2019-12-17 07:02:47
问题 What is the difference, if there is one, between a destructor and a Finalize method in a class? I recently discovered that Visual Studio 2008 considers a destructor synonymous with a Finalize method, meaning that Visual Studio won't let you simultaneously define both methods in a class. For example, the following code fragment: class TestFinalize { ~TestFinalize() { Finalize(); } public bool Finalize() { return true; } } Gives the following error on the call to Finalize in the destructor: The

Destructors of builtin types (int, char etc..)

五迷三道 提交于 2019-12-17 06:33:27
问题 In C++ the following code gives a compiler error: void destruct1 (int * item) { item->~int(); } This code is nearly the same, I just typedef the int to another type and something magic happens: typedef int myint; void destruct2 (myint * item) { item->~myint(); } Why does the second code work? Does an int get a destructor just because it has been typedefed? In case you wonder why one ever would like to do this: This comes from refactoring C++ code. We're removing the standard heap and

Why don't STL containers have virtual destructors?

强颜欢笑 提交于 2019-12-17 05:54:28
问题 Does anyone know why the STL containers don't have virtual destructors? As far as I can tell, the only benefits are: it reduces the size of an instance by one pointer (to the virtual method table) and it makes destruction and construction a tiny bit faster. The downside is that it's unsafe to subclass the containers in the usual way. EDIT: Perhaps my question could be rephrased "Why weren't STL containers designed to allow for inheritance?" Because they don't support inheritance, one is stuck

When is an object “out of scope”?

你说的曾经没有我的故事 提交于 2019-12-17 04:26:29
问题 In C++, when is an object defined as "out of scope"? More specifically, if I had a singly linked list, what would define a single list node object as "out of scope"? Or if an object exists and is being referenced by a variable ptr , is it correct to say that the object is defined as "out of scope" the moment the reference is deleted or points to a different object? UPDATE: Assuming an object is a class that has an implemented destructor. Will the destructor be called the moment the object

Is calling destructor manually always a sign of bad design?

拟墨画扇 提交于 2019-12-17 02:45:09
问题 I was thinking: they say if you're calling destructor manually - you're doing something wrong. But is it always the case? Are there any counter-examples? Situations where it is neccessary to call it manually or where it is hard/impossible/impractical to avoid it? 回答1: Calling the destructor manually is required if the object was constructed using an overloaded form of operator new() , except when using the " std::nothrow " overloads: T* t0 = new(std::nothrow) T(); delete t0; // OK: std:

When should I prevent implicit destruction? How does it work?

南楼画角 提交于 2019-12-14 04:01:25
问题 I know that I can declare a destructor =delete or private in order to prevent the program from implicitly deleting the object at the end of scope. I also know that if it's private, I can have a member function that can explicitly call the destructor whenever I call it: void kill() { this–>~A(); } My questions are: Why would I ever want to prevent implicit destruction? Please give an example What would =delete do? Does it make sure the destructor never runs? So the object will exist forever

Destroying the form [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-14 03:38:01
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . How can I do that? Under destroying I mean remove it from the memory . An example (Form1 is some form): static void Main() { Test(); // here we still have A alive // GC.Collect() doesn't helps Form1 B = new Form1(); Application.Run(B); // problem is here: B and A "collides", due to assumption what

c++ adding objects to vector destroys earlier objects

泪湿孤枕 提交于 2019-12-13 23:46:20
问题 I need to add objects of the same class to a vector: #include <vector> #include <cstdio> class A { int *array; int size; public: A(int s) { array = new int[size = s]; fprintf(stderr, "Allocated %p\n", (void*)array); } ~A() { fprintf(stderr, "Deleting %p\n", (void*)array); delete array; } }; int main() { std::vector<A> v; for (int n = 0; n < 10; n++) { fprintf(stderr, "Adding object %d\n", n); v.push_back(A(10 * n)); //v.emplace_back(10 * n); } return 0; } When I run this program, it crashes

How to change HTTP response and show an error message when exception happens in destructor?

不羁的心 提交于 2019-12-13 17:50:24
问题 I have a situation where a PHP function attempts to redirect the browser via an HTTP 302, but an exception is being thrown in a destructor called upon 'exit'ing. The actual code in question is the _doRedirect() method of SimpleSAML but here's a simplified situation: header('Location: http://somewhere.com', TRUE, 302); exit; // end script execution The 'exit' is triggering a destructor for an unrelated class and the error details are getting written out to the HTTP response... but no human

std::vector destructor gives error

扶醉桌前 提交于 2019-12-13 11:17:43
问题 I have a class like this: class Foo { public: Foo() {}; ~Foo() {}; void MyFunc(int a) { m_struct.my_vec.push_back(a); } public: MyStructType m_struct; } and MyStructType is defined similar to this: struct MyStructType { std::vector<int> my_vec; } The problem is that when I instantiate the class as follows, I get a memory violation error for std::vector deallocation when the m_struct destructor is called: void main() { Foo f; f.m_struct.my_vec.push_back(10); } However, if I do it the following