# delete

Definition: | delete(object o)
Description: | Invoke any cleanup routines associated with the object and remove the association.
pwa/p2js: | Not supported - you cannot put a delete_routine() on anything anyway.
Comments: | Cleanup routines are associated with an object using the delete_routine() function. Applications do not typically invoke delete() directly, but instead rely on it being invoked as part of the automatic garbage collection process, except during shutdown when it may be important to force events to occur in a particular order. Note that setting [the last copy of] o to NULL should achieve the same effect as invoking delete() explicitly, but in a much safer way, as per the comments in delete_routine(). If the object is an integer, or if no cleanup routines are associated with the object, then nothing happens. After the cleanup routines are called, the value of the object is unchanged, though the cleanup routine(s) will no longer be associated with the object.
Ancilliary function: | bool res = still_has_delete_routine(object x, integer specifically=0) Yields true(1) if delete_routine() has been invoked on x and delete() has not. Yields false(0) after delete() or if delete_routine() was never called on x. If specifically is provided/non-zero it will also return false when x has some other delete_routine attached. In particular, residual references to a class instance become invalid the moment delete() is invoked on any other reference to it. The internal type checking routines of builtins\structs.e use this to identify and prohibit accidental access to any such now-invalid struct/class instance references. Ideally you should nullify all variables referencing x as and when delete(x) is invoked, however that may not always be possible, as suggested in the example below, albeit something that trivial is trivially fixable, by (for example) making process a null-returning function and invoking it as w = process(w).
Example: |
    class thing nullable
        public string s
    end class

    procedure process(thing t)
        delete(t)   -- (w becomes invalid)
    end procedure

    thing w = new({"w"})
    process(w)
    ?w.string       -- (invalid)
The delete(t) causes w to become invalid and refer to a slot on the freelist. While a fatal error would normally occur on the last line, it was (prior to the introduction of still_has_delete_routine) possible for some other instance to be created and occupy the slot that w still refers to. This additional check seeks to prevent mishaps of that kind. Of course just printing the contents would probably have been fairly harmless, modifications and/or actions on such an invalid reference would however be significantly more dangerous and damaging.
Implementation: | via :%opDelete in builtins\VM\pDeleteN.e (an autoinclude) - be warned however it is low-level complicated stuff that you do not need to know. Apart from still_has_delete_routine(), which is defined in builtins\hasdel.e
See Also: | delete_routine