self-destruction

Suicide: Objective-C objects calling their own -dealloc methods on themselves

谁说胖子不能爱 提交于 2019-12-06 14:14:25
问题 Is it good practice for an object in Objective-C to commit suicide? That is, for an object to declare [self dealloc] where -dealloc permits an orderly wind down as usual? What are the principal risks? As it happens I have a specific example, a custom timer object that extends NSObject and comprises an NSTimer instance and an NSUInteger which is set to limit the number of times the timer fires. When time is up the object tells the timer to -invalidate and then commits suicide by calling its

What will happen if a std::vector element 'commits suicide' (using delete this;)?

主宰稳场 提交于 2019-12-05 02:59:26
问题 Suppose there's a vector of Item s vector<Item*> items; //{item1, item2, item3} Then, in other part of the code, items[1]->suicide(); where the suicide function is: void Item::suicide() { delete this; } What is items vector size and how it's arrangement now? It is okay to do this? Edit (may I ask an additional question?): If the desired arrangement of the output is {item1, item3} , size is 2 , and no dangling pointer, how to do it in a self-destructing way (from the item2 itself)? Edit 2 :

Self deleting bash script

删除回忆录丶 提交于 2019-12-05 00:04:02
How can a bash script execute even after encountering a statement to delete itself? For eg when I ran test.sh script which conains: <--some commands--> rm test.sh <--some more commands--> end The script executes till the end before deleting itself What actually happens is that bash keeps the file open and rm won't make that stop. So rm calls the libc function "unlink()" which will remove the "link" to the inode from the directory it's in. This "link" is in fact a filename together with an inode number (you can see inode numbers with ls -i ). The inode exists for as long as programs have it

Suicide: Objective-C objects calling their own -dealloc methods on themselves

元气小坏坏 提交于 2019-12-04 19:11:57
Is it good practice for an object in Objective-C to commit suicide? That is, for an object to declare [self dealloc] where -dealloc permits an orderly wind down as usual? What are the principal risks? As it happens I have a specific example, a custom timer object that extends NSObject and comprises an NSTimer instance and an NSUInteger which is set to limit the number of times the timer fires. When time is up the object tells the timer to -invalidate and then commits suicide by calling its -dealloc method. As this is automatic we have no worries about having to track the object or crucially

What will happen if a std::vector element 'commits suicide' (using delete this;)?

穿精又带淫゛_ 提交于 2019-12-03 17:32:10
Suppose there's a vector of Item s vector<Item*> items; //{item1, item2, item3} Then, in other part of the code, items[1]->suicide(); where the suicide function is: void Item::suicide() { delete this; } What is items vector size and how it's arrangement now? It is okay to do this? Edit (may I ask an additional question?): If the desired arrangement of the output is {item1, item3} , size is 2 , and no dangling pointer, how to do it in a self-destructing way (from the item2 itself)? Edit 2 : Thanks for all the answers! Awesome. So I finally decided and found the way to do it from outside of the

How to make scripts auto-delete at the end of execution?

无人久伴 提交于 2019-11-30 13:17:24
问题 Is it possible to make a python script that will delete the .py file at the end of its execution (self-delete) in windows? 回答1: I'm not sure deleting a file while it's in memory would be a good idea. Try running a batch file from the script which closes the script process, then deletes the script file. There may be a native method to self destruct a script, but I am not aware of it. EDIT: Here is a simple example of how you could accomplish this using the method I described: In the script # C

PHP file that should run once and delete itself. Is it possible?

為{幸葍}努か 提交于 2019-11-30 12:49:05
问题 Is it possible to create a PHP file that runs once with no errors and deletes itself? 回答1: <?php unlink(__FILE__); ?> 回答2: Here's a great way of ensuring the script gets deleted, no matter if intervening code calls exit() or not. class DeleteOnExit { function __destruct() { unlink(__FILE__); } } $g_delete_on_exit = new DeleteOnExit(); 回答3: unlink() is the valid function for this, but sometimes it is useful to refer to functions and variables in base classes or to refer to functions in classes

How to make scripts auto-delete at the end of execution?

三世轮回 提交于 2019-11-30 06:46:45
Is it possible to make a python script that will delete the .py file at the end of its execution (self-delete) in windows? I'm not sure deleting a file while it's in memory would be a good idea. Try running a batch file from the script which closes the script process, then deletes the script file. There may be a native method to self destruct a script, but I am not aware of it. EDIT: Here is a simple example of how you could accomplish this using the method I described: In the script # C:\test.py import os os.startfile(r"C:\sampleBatch.bat") In the batch # C:\sampleBatch.bat TASKKILL /IM

PHP file that should run once and delete itself. Is it possible?

本小妞迷上赌 提交于 2019-11-30 04:54:28
Is it possible to create a PHP file that runs once with no errors and deletes itself? <?php unlink(__FILE__); ?> Here's a great way of ensuring the script gets deleted, no matter if intervening code calls exit() or not. class DeleteOnExit { function __destruct() { unlink(__FILE__); } } $g_delete_on_exit = new DeleteOnExit(); unlink() is the valid function for this, but sometimes it is useful to refer to functions and variables in base classes or to refer to functions in classes that have not yet any instances. class SelfDelete{ public static $obj; function __destruct(){ unlink(__FILE__); }

Is it valid to directly call a (virtual) destructor?

China☆狼群 提交于 2019-11-29 13:44:20
In this answer , Ryan directly calls the virtual destructor. I've tested the code in VS2010, and it correctly calls all destructors (tested with logging statements). Is it actually valid to do so? What are the problems, flaws or even the good points of such an approach? I can only think of it as a way to really force a reset of the actual type, even if they don't override a virtual reset function, since they atleast have to clean up in their destructors. Also, eactly what kind of side-effects does a call to the destructor bring? Is it undefined behaviour to use the object after such a