self-destruction

“delete this” in constructor

十年热恋 提交于 2019-11-28 22:48:21
What actually happen when I execute this code? class MyClass { MyClass() { //do something delete this; } } It turns out that in this particular case the code is legal, but you're ε-away from undefined behavior. The C++ standard defines the notion of the "lifetime" of an object to be the time between which its constructor has finished running and when the destructor starts running. It also explicitly states (in §3.8/5) that Before the lifetime of an object has started [...] If the object will be or was of a class type with a non-trivial destructor, and the pointer is used as the operand of a

Object-Oriented Suicide or delete this;

女生的网名这么多〃 提交于 2019-11-28 10:04:49
The following code compiled with MSVC9.0 runs and outputs Destructor four times, which is logical. #include <iostream> class SomeClass { public: void CommitSuicide() { delete this; } void Reincarnate() { this->~SomeClass(); new (this) SomeClass; } ~SomeClass() { std::cout << "Destructor\n"; } }; int main() { SomeClass* p = new SomeClass; p->CommitSuicide(); p = new SomeClass; p->Reincarnate(); p->~SomeClass(); //line 5 p->CommitSuicide(); } I think the first 4 lines of code in main do not result in undefined behavior (although not entirely sure about the delete this; thing). I would like to

How can I make my .NET application erase itself?

旧城冷巷雨未停 提交于 2019-11-28 05:53:05
How can I make my C# app erase itself (self-destruct)? Here's two ways that I think might work: Supply another program that deletes the main program. How is this deleter program deleted then, though? Create a process to CMD that waits a few seconds then deletes your file. During those few seconds, you close your application. Both of those methods seem inefficient. I have a feeling that there's some built-in flag or something in Windows that allows for such stuff. How should I do it? Also, can you provide some sample code? UPDATE: Thanks for all your answers! I'm going to try them, and see

Is it OK to use “delete this” to delete the current object?

元气小坏坏 提交于 2019-11-28 01:09:05
I'm writing a linked list and I want a struct's destructor (a Node struct) to simply delete itself, and not have any side effects. I want my list's destructor to iteratively call the Node destructor on itself (storing the next node temporarily), like this: //my list class has first and last pointers //and my nodes each have a pointer to the previous and next //node DoublyLinkedList::~DoublyLinkedList { Node *temp = first(); while (temp->next() != NULL) { delete temp; temp = temp->next(); } } So this would be my Node destructor: Node::~Node { delete this; } Is this acceptable, especially in

Self-destructing application

僤鯓⒐⒋嵵緔 提交于 2019-11-28 00:45:06
Along the lines of "This tape will self-destruct in five seconds. Good luck, Jim" ... Would it be possible for an application to delete itself (or it's executable wrapper form) once a preset time of use or other condition has been reached? Alternatively, what other approaches could be used to make the application useless? The aim here is to have a beta expire, inviting users to get a more up-to-date version. Stephen C It is possible. To get around the lock on the JAR file, your application may need to spawn a background process that waits until the JVM has exited before deleting stuff. However

What is the use of “delete this”?

我只是一个虾纸丫 提交于 2019-11-27 19:42:13
Today, I have seen some legacy code. In the destructor there is a statement like " delete this ". I think, this call will be recursive. Why it is working? I made some quick search on Y!, I found that if there is a need to restrict the user to create the stack object, we can make destructor private and provide an interface to delete the instance. In the interface provided, we have to call delete on this pointer. Are there any other situations for using such statements? JaredPar "delete this" is commonly used for ref counted objects. For a ref counted object the decision of when to delete is

Should “delete this” be called from within a member method?

会有一股神秘感。 提交于 2019-11-27 14:37:58
I was just reading this article and wanted SO folks advice: Q: Should delete this; be called from within a member method? Normally this is a bad idea, but it's occasionally useful. It's perfectly safe as long as you don't use any member variables after you delete, and as long as clients calling this method understand it may delete the object. A good example of when this is useful is if your class employs reference counting: void Ref() { m_References++; } void Deref() { m_References--; if (m_References == 0) { delete this; } } I think there are really 2 questions here Can delete this be validly

“delete this” in constructor

折月煮酒 提交于 2019-11-27 14:27:52
问题 What actually happen when I execute this code? class MyClass { MyClass() { //do something delete this; } } 回答1: It turns out that in this particular case the code is legal, but you're ε-away from undefined behavior. The C++ standard defines the notion of the "lifetime" of an object to be the time between which its constructor has finished running and when the destructor starts running. It also explicitly states (in §3.8/5) that Before the lifetime of an object has started [...] If the object

Should objects delete themselves in C++?

旧街凉风 提交于 2019-11-27 12:24:27
I've spent the last 4 years in C# so I'm interested in current best practices and common design patterns in C++. Consider the following partial example: class World { public: void Add(Object *object); void Remove(Object *object); void Update(); } class Fire : Object { public: virtual void Update() { if(age > burnTime) { world.Remove(this); delete this; } } } Here we have a world responsible for managing a set of objects and updating them regularly. Fire is an an object that might be added to the world under many different circumstances but typically by another object already in the world. Fire

Self deletable application in C# in one executable

不想你离开。 提交于 2019-11-27 06:41:25
Is it possible to make an application in C# that will be able to delete itself in some condition. I need to write an updater for my application but I don't want the executable to be left after the update process. There is an official .Net OneClick but due to some incompatibilities with my HTTP server and some problems of OneClick itself I'm forced to make one myself. George. [EDIT] In more details: I have: Application Executable which downloads the updater ("patch", but not exactly) this "patch" updates the application executable itself. Application executes as folowed: Application: Start ->