Git Product home page Git Product logo

elisp-finalize's Introduction

Finalizers for Emacs Lisp

NOTE: Emacs 25 directly supports finalizers via make-finalizer. Only use this package if you must support Emacs 24 or earlier.

This package provides finalizers for Emacs Lisp objects. Objects registered with this package will have a specified finalizer function run immediately after that object is garbage collected.

The API has one function: finalize-register. It accepts an object, a finalizer, and arguments to be passed to the finalizer. The object being finalized will be unavailable to the finalizer.

This package works by taking advantage of weak references and post-gc-hook.

See also: Emacs Lisp Object Finalizers

Usage

Use delete-process as a finalizer to clean up a leftover process.

(require 'cl-lib)
(require 'finalize)

(cl-defstruct (pinger (:constructor pinger--create))
  process host)

(defun pinger-create (host)
  (let* ((process (start-process "pinger" nil "ping" host))
         (object (pinger--create :process process :host host)))
    (finalize-register object #'delete-process process)
    object))

(setf pinger (pinger-create "localhost"))
;; => [cl-struct-pinger #<process pinger> "localhost"]

(get-process "pinger")
;; => #<process pinger>

;; Allow object to be garbage collected.
(setf pinger nil)
(garbage-collect)

;; Process has been automatically cleaned up by the finalizer.
(get-process "pinger")
;; => nil

Or using the finalizable EIEIO mixin class, which calls finalize on a copy of the original object after garbage collection.

(require 'eieio)
(require 'finalizable)

(defclass pinger (finalizable)
  ((process :initarg :process :reader pinger-process)
   (host :initarg :host :reader pinger-host)))

(defun pinger-create (host)
  (make-instance 'pinger
                 :process (start-process "ping" nil "ping" host)
                 :host host))

(defmethod finalize ((pinger pinger))
  (delete-process (pinger-process pinger)))

Closure Caveat

Be mindful when using lexical scope and passing a lambda to finalize-register. Uncompiled lambdas capture their entire environment, which almost certainly includes the object subject to finalization. This will backfire and keep the object alive indefinitely. This situation will only work correctly when your function is byte-compiled, which will provide precise lexical environment capture.

elisp-finalize's People

Contributors

skeeto avatar tarsius avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

emacsmirror

elisp-finalize's Issues

Support Emacs 25.1 finalizers

I've added core support for finalizers to Emacs 25.1; it would be nice if this package used the core functionality if available. The core support has special GC hooks that solve the cyclic reference problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.