When creating markers, should code that may fail (a user defined function for example) clean-up after it's self?

So for example, new markers are created, the function of unknown origin is called, on error, set all markers positions to nil so they are garbage collected.

Is this important, or is going out of scope (with a let binding for example) enough to have these markers be garbage collected without the need for manual cleanup?

Here is a concrete example:

Will garbage collection handle this case?

(let ((marker (set-marker (make-marker) 14)))
  ;; This function might fail.
  (funcall user-defined-fn))

Or is something like this needed?

(let ((marker (set-marker (make-marker) 14)))
  ;; This function might fail.
    (funcall user-defined-fn))
    ;; Ensure garbage collection.
    (set-marker marker nil))

https://www.gnu.org/software/emacs/manual/html_node/elisp/Overview-of-Markers.html notes:

Insertion and deletion in a buffer must check all the markers and relocate them if necessary. This slows processing in a buffer with a large number of markers. For this reason, it is a good idea to make a marker point nowhere if you are sure you don’t need it any more. Markers that can no longer be accessed are eventually removed (see Garbage Collection).

See also this related question.

  • Markers are user features, so use them in lisp code only if necessary and if you do use them and they are transient, that is they are only used by your code then yes, you have to remove them, otherwise they stay in the buffer.
    – Tom
    Commented Oct 28, 2021 at 7:28
  • Please try to ask a specific how-to question. The title sounds like how-to, but is too general. And the question itself invites opinion-based answers. E.g. what's behind "should" - could be a world of discussion, without necessarily going anywhere. Specific questions are good questions - show a specific problem with markers and ask for ways (not "should" or "best") to solve that problem. (This is my suggestion.)
    – Drew
    Commented Oct 28, 2021 at 15:44
  • 1
    Added a concrete example.
    – ideasman42
    Commented Oct 28, 2021 at 20:41


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.