Previous: Initialization, Up: Life Cycle


3.3.2 Finalization

A finalization function conFinalizer is registered via the external pointer mechanism when the connection is created. This function is executed when a garbage collection event finds that the connection is no longer reachable from the running R program. For more information on the finalization and external pointer mechanism, see the Writing R Extensions manual. The function conFinalizer calls con_destroy, which calls con_close1. The con_close1 function calls the close and destroy methods specified in the struct Rconn structure associated with the connection, if necessary, and deallocates memory associated with character re-encoding and the description and class character strings. Finally, on return to con_destroy, memory associated with the struct Rconn structure is released.

The function con_destroy may also be called if the open method of an initialized connection fails, and is always called following a call to the R function close on non-standard, non-sink connections.