Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8825871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:59:06+00:00 2026-06-14T06:59:06+00:00

In common lisp, what is the preferred way to manage external resources (sockets, filesystem

  • 0

In common lisp, what is the preferred way to manage external resources (sockets, filesystem handles, etc)?

I’m trying to make a simple opengl 2d platformer in common lisp.
The problem is I’m not quite sure how to track OpenGL textures (must be deleted with glDeleteTextures when they are no longer needed).

In C++ I preferred to use following scheme:

  1. Make texture class
  2. Make smart/weak pointers for that texture class
  3. Store textures in map (dictionary/hashtable) that maps file names to weak pointers to textures.
  4. When new textures are requested, look into map, and see if there’s a non-null (nil) weak pointer available. If it is available, return existing object, otherwise load new texture.

However, I’m not quite sure how to port this scheme to common lisp, because:

  1. There are no destructors.
  2. There’s garbage collector, and it seems my implementation (clozureCL on windows platform) supports finalizers, but as far as I can tell it is not recommended to use finalizers in common lisp because they’re not deterministic.
  3. The preferred way of managing resources using (with-* doesn’t look suitable there, because resources can be shared, and loaded/unloaded in the middle of function call.

As far as I can tell, there are several approaches available:

  1. Give up on automatic resource managment, and do it manually.
  2. Implement something similar to C++ RAII, weakpointer and smartpointer using macros (this code probably doesn’t work):

    (defclass destructible () ())
    
    (defmethod destroy ((obj destructible)) (print (format nil "destroy: ~A" obj)))
    
    (defparameter *destructible-classes-list* nil)
    
    (defmethod initialize-instance :after ((obj destructible) &key)
      (progn
          (push *destructible-classes-list* obj)
          (print (format nil "init-instance: ~A" obj))))
    
    (defmacro with-cleanup (&rest body)
      `(let ((*destructible-classes-list* nil))
        (progn ,@body (mapcar (lambda (x) (destroy x)) *destructible-classes-list*))))
    
    (defclass refdata (destructible)
      ((value :accessor refdata-value :initform nil)
       (ref :accessor refdata-ref :initform 0)
       (weakrefcount :accessor refdata-weakref :initform 0)))
    
    (defmethod incref ((ref refdata))
      (if ref (incf (refdata-ref ref))))
    
    (defmethod decref ((ref refdata))
      (if ref
        (progn (decf (refdata-ref ref))
         (if (<= (refdata-ref ref) 0) 
           (progn (destroy (refdata-value ref))
              (setf (refdata-value ref) nil))))))
    
    (defmethod incweakref ((ref refdata))
      (if ref (incf (refdata-weakref ref))))
    
    (defmethod decweakref ((ref refdata))
      (if ref (decf (refdata-weakref ref))))
    
    (defclass refbase (destructible) ((data :accessor refbase-data :initform nil)))
    
    (defmethod deref ((ref refbase))
      (if (and (refbase-data ref) (refdata-value (refbase-data ref)))
        (refdata-value (refbase-data ref))
        nil))
    
    (defclass strongref (refbase) ())
    
    (defclass weakref (refbase) ())
    
    (defmethod destroy :before ((obj strongref))
      (decref (refbase-data obj)))
    
    (defmethod destroy :before ((obj weakref))
      (decweakref (refbase-data obj)))
    
    (defmethod initialize-instance :after ((obj strongref) &key)
      (incref (refbase-data obj)))
    
    (defmethod initialize-instance :after ((obj weakref) &key)
      (incweakref (refbase-data obj)))
    

Is there a better way to do it?

C++ Concepts Explanation: What is a smart pointer and when should I use one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:59:07+00:00Added an answer on June 14, 2026 at 6:59 am

    If you want to handle a dynamic extent scope use UNWIND-PROTECT. If the program leaves that scope – normally or on error – the clean-up form is called. Just deallocate there or do whatever you want.

    Sometimes Lisps use some kind of ‘resource’ mechanism to keep track of used and unused objects. Such a library provides fast allocation from a pool, allocating, initialization, deallocation, mapping of resourced object. CLIM defines a primitive version: Resources. CCL has a similar primitive version of it.

    In a Lisp with ‘timers’, you can run a function periodically which looks for ‘objects’ to deallocate. In CCL you can use a thread which sleeps for a certain time using PROCESS-WAIT.

    A bit about your coding style:

    • FORMAT can directly output to a stream, no PRINT needed
    • (defmethod foo ((e bar)) (if e ...)) : the IF makes no sense. e will always be an object.
    • many PROGN are not needed. Where needed, it could be removed when IF is replaced by WHEN.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a simple bubble sort in Common Lisp (also works
I just started learning Common Lisp a few days ago, and I'm trying to
I am familiar with Common Lisp and trying to learn some Scheme, so I
I am teaching myself Common Lisp and I'm creating a simple program that interfaces
I have started working my way through Practical Common Lisp and am looking for
I enjoy common lisp, but sometimes it is really painful to input simple math
I'm new to Common Lisp, and found myself taking advantage of way functions returns
Newbie Common Lisp question here. Is there a way to reset the state of
I'm trying out lisp and working through the book Practical Common Lisp at http://www.gigamonkeys.com/book/
I'm trying to learn Common Lisp (sbcl) and getting practice with basic defuns. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.