1# $EPIC: defer,v 1.2 2005/03/11 04:55:04 jnelson Exp $ 2Synopsis: 3 defer {<stuff>} 4 5Description: 6 The DEFER command schedules <stuff> to be executed later on when 7 the client is idle, and there is no risk that if <stuff> were to destroy 8 something that it would crash the client. 9 10 Any time you want to destroy something, whether it be a DCC, a server, 11 a open file, an /on, a window, a log file, etc, you always run the risk 12 that some alias further down the stack might have done a /wait or a 13 /pause or a /redirect, and is holding a reference to the thing you want 14 to destroy. It is possible that if you destroy that thing, and then the 15 /wait or /pause or /redirect finishes, that the alias down further in the 16 stack might try to use the object, which you destroyed, and thus it will 17 crash the client. 18 19 Now, I'm not saying that such cases are "normal" but there are sequencing 20 issues that may result in it being dangerous for you to destroy something 21 at certain times. THIS IS ESPECIALLY TRUE INSIDE ONs. If you find that 22 your script can crash the client by deleting something, the first thing 23 you should try is to put a /DEFER in front of the command to see if that 24 fixes it. 25 26 Obviously, if you /DEFER destroying something, it won't be destroyed 27 immediately, so you can't use it in any place where it's important that 28 the object is actually destroyed before you continue. 29 30Example 1: 31 Here's an example that will malfunction on many versions: 32 33 ON #401 -1 * { on 401 - } 34 ON ^401 * { echo -- 401 hooked! $* 35 whois doesnotexist 36 37 The reason why this crashes is because the -1 serial numbered on deletes 38 both on's, but the /ON system still has a pointer to the second on and 39 still intends to run it, even after it's been deleted. Although this 40 particular bug has been fixed in recent versions, it's an example of the 41 kind of destruction that could cause a crash. 42 43 So in the above example, this is the "safer" way: 44 45 ON #401 -1 * { defer on 401 - } 46 ON ^401 * { echo -- 401 hooked! $* 47 whois doesnotexist 48 49 But obviously this means that the second /on will execute. (There is no 50 reasonable, portable, crash-proof way to destroy an /on so it will not 51 run from a lower numbered serial number.) 52 53Example 2: 54 It doesn't matter what you want to do within an /ON WINDOW_* hook, you 55 probably should /DEFER it. 56 57History: 58 The DEFER command first appeared in EPIC4-0.9.16. 59 60