The R Project SVN R

Rev

Rev 22985 | Rev 37702 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22985 Rev 25101
Line 1... Line 1...
1
\name{reg.finalizer}
1
\name{reg.finalizer}
2
\alias{reg.finalizer}
2
\alias{reg.finalizer}
3
\title{Finalization of objects}
3
\title{Finalization of objects}
4
\description{
4
\description{
5
  Registers an \R{} function to be called upon garbage collection of object. 
5
  Registers an \R function to be called upon garbage collection of
-
 
6
  object.
6
}
7
}
7
\usage{
8
\usage{
8
  reg.finalizer(e, f)
9
  reg.finalizer(e, f)
9
}
10
}
10
\arguments{
11
\arguments{
11
  \item{e}{Object to finalize. Must be environment or external pointer.}
12
  \item{e}{Object to finalize. Must be environment or external pointer.}
12
  \item{f}{Function to call on finalization. Must accept a single argument, 
13
  \item{f}{Function to call on finalization. Must accept a single argument, 
13
     which will be the object to finalize.}
14
    which will be the object to finalize.}
14
}
15
}
15
\value{
16
\value{
16
  \code{NULL}.
17
  \code{NULL}.
17
}
18
}
-
 
19
\note{
18
\note{The purpose of this function is mainly to allow objects that 
20
  The purpose of this function is mainly to allow objects that refer to
19
   refer to external items (a temporary file, say) 
21
  external items (a temporary file, say) to perform cleanup actions when
20
   to perform cleanup actions when they are no 
-
 
21
   longer referenced from within \R{}. This only makes sense for objects 
22
  they are no longer referenced from within \R.  This only makes sense
22
   that are never copied on assignment, hence the restriction to environments 
23
  for objects that are never copied on assignment, hence the restriction
23
   and external pointers.}
24
  to environments and external pointers.
24
 
25
}
25
\examples{
26
\examples{
26
f <- function(e) print("cleaning....")
27
f <- function(e) print("cleaning....")
27
g <- function(x){e<-environment(); reg.finalizer(e,f)}
28
g <- function(x){e<-environment(); reg.finalizer(e,f)}
28
g() 
29
g() 
29
invisible(gc()) # trigger cleanup
30
invisible(gc()) # trigger cleanup