Skip to content

on.exit processing

3 messages · james.holtman@convergys.com, Luke Tierney

#
I have encountered a 'strange' behavior in the 'on.exit' processing.  I had
a function where I setup an 'on.exit' condition and then later on added to
it with an 'on.exit({...}, add=T)'.  What appeared to happen is that on
subsequent calls to the function, even if only the first 'on.exit' was
called, it was still executing the one with the 'add=T'.

I am using Version 1.3.0  (2001-06-22) on Windows 2000.

Here is a small sample of what is happening.  The function conditionally
sets up the on.exit, and as you can see, each time the 'second' one is
setup, it just keeps adding to what is output:
+     on.exit({cat('first exit call\n')})
+     if (x == 1) on.exit({cat('second exit call\n')}, add=T)
+     invisible(NULL)
+ }
first exit call
first exit call
second exit call
first exit call
second exit call         #<<  shouldn't be here
first exit call
second exit call
second exit call         #<<  shouldn't be here
first exit call
second exit call
second exit call         #<<  shouldn't be here
second exit call         #<<  shouldn't be here
first exit call
second exit call
second exit call         #<<  shouldn't be here
second exit call         #<<  shouldn't be here
second exit call         #<<  shouldn't be here
--

NOTICE:  The information contained in this electronic mail transmission is
intended by Convergys Corporation for the use of the named individual or
entity to which it is directed and may contain information that is
privileged or otherwise confidential.  If you have received this electronic
mail transmission in error, please delete it from your system without
copying or forwarding it, and notify the sender of the error by reply email
or by telephone (collect), so that the sender's address records can be
corrected.


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Yuk!

I think the problem is in the way on.exit actions are installed.
Looking at the output of debug it looks like we are doing the add by
destructively modifying the expression, which in turn modifies the
function code itself:
first exit call
first exit call
second exit call
first exit call
second exit call
second exit call
debugging in: f(1)
debug: {
    on.exit({
        cat("first exit call\n")
        {
            cat("second exit call\n")
        }
        {
            cat("second exit call\n")
        }
    })
    if (x == 1) 
        on.exit({
            cat("second exit call\n")
        }, add = T)
    invisible(NULL)
}
Browse[1]> 
debug: on.exit({
    cat("first exit call\n")
    {
        cat("second exit call\n")
    }
    {
        cat("second exit call\n")
    }
})

Looking at the code it would appear that this only happens when the
existing on.exit form has braces around it. Looks like the code


		ctxt->conexit = listAppend(oldcode,tmp);

in builtin.c:do_onexit should be something like

		ctxt->conexit = listAppend(duplicate(oldcode),tmp);

but this needs a bit more checking.

luke
On Mon, Sep 10, 2001 at 11:01:39AM -0400, james.holtman at convergys.com wrote:

  
    
#
Replacing

 		ctxt->conexit = listAppend(oldcode,tmp);

in builtin.c:do_onexit with

 		ctxt->conexit = listAppend(duplicate(oldcode),tmp);

fixes this bug.  It is fixed in the development tree and should be in
the next release.

If you need to work around the bug before then, make sure the first
on.exit expression you register does not have braces around it.

luke