If it's just a one-off, you can always just write a macro that wraps around dynamic-wind tuning and disabling in before and after thunks (I assume that allocate-vertex-buffer-object and free-vertex-buffer-object your constructor and destructors here):
(define-syntax with-vertex-buffer-object (syntax-rules () ((_ (name arg ...) body ...) (let ((name
If this is a template that you use a lot for different types of objects, you can write a macro to create such a macro; and you probably want to highlight a series of them at a time, so you might want to have a list of bindings at the beginning, not just one.
Out of the cuff here, a more general version; I'm not quite sure of the name, but it demonstrates the basic idea (edited to fix an infinite loop in the original version):
(define-syntax with-managed-objects (syntax-rules () ((_ ((name constructor destructor)) body ...) (let ((name
And you will use it as follows:
(with-managed-objects ((vbo (allocate-vertex-buffer-object 1 2 3) (free-vertext-buffer-object vbo)) (frob (create-frobnozzle 'foo 'bar) (destroy-frobnozzle frob))) ;; do stuff ... )
Here is an example that demonstrates that it works, including exiting and re-entering an area using continuations (this is a pretty far-fetched example, apologies if the control flow is a little difficult to execute):
(let ((inner-continuation #f)) (if (with-managed-objects ((foo (begin (display "entering foo\n") 1) (display "exiting foo\n")) (bar (begin (display "entering bar\n") (+ foo 1)) (display "exiting bar\n"))) (display "inside\n") (display "foo: ") (display foo) (newline) (display "bar: ") (display bar) (newline) (call/cc (lambda (inside) (set! inner-continuation inside) #t))) (begin (display "* Let try that again!\n") (inner-continuation #f)) (display "* All done\n")))
This should print:
entering foo
entering bar
inside
foo: 1
bar: 2
exiting bar
exiting foo
* Let try that again!
entering foo
entering bar
exiting bar
exiting foo
* All done
call/cc is simply an abbreviation for call-with-current-continuation ; use a longer form if your circuit does not have a shorter one.
Update . As you explained in your comments, you are looking for a way to manage resources that can be returned from a specific dynamic context. In this case, you will have to use the finalizer; finalizer is a function that will be called by the object as soon as the GC has proved that it cannot be reached anywhere. Finalizers are not standard, but most mature System Systems have them, sometimes under different names. For example, in the PLT diagram, see Wills and Executors .
You must remember that in a Schema, a dynamic context can be re-entered; this is different from most other languages ββin which you can exit the dynamic context at any arbitrary point using exceptions, but you cannot re-enter. In my example above, I demonstrated a naive approach to using dynamic-wind to free resources when leaving a dynamic context and reallocate them if you re-enter. This may be appropriate for some resources, but for many resources it would be impractical (for example, reopening a file, now you will be at the beginning of the file when you re-enter the dynamic context) and can have significant overhead.
Taylor Campbell (yes, there is a relationship) has an article on his blog (entry 2009-03-28) that addresses this issue and presents several alternatives based on the exact semantics you want. For example, it provides an unwind-protext that will not invoke a cleanup procedure until it is possible to re-enter the dynamic context in which the resource is available.
Thus, it covers the many different options available. There is no exact correspondence to RAII, since Scheme is a completely different language and has very different restrictions. If you have a more specific use case or more detailed information about the used case, which you spoke briefly about, I can provide you with more specific recommendations.