chickadee » jiffi » nullify-armor!

nullify-armor! armorprocedure

Sets armor's data to #f (which is treated as a null pointer) and returns armor. armor must be an instance of any type defined with define-armor-type.

This procedure also recursively nullifies armor's children (if it has any), unless armor does not track its children.

Struct freers and array freers will automatically call this procedure for you. You should call this procedure manually to nullify an armor when its data becomes unusable for other reasons, for example when calling a destructor C function.

(define-binding FOO_DestroyBlackbox
  args: ((c-pointer bb)))

(define (destroy-blackbox! bb)
  ;; FOO_DestroyBlackbox frees bb's data, but if bb is an armor it
  ;; will still wrap a pointer to the old address, which could cause
  ;; a memory access violation if bb is used again later.

  ;; Unwrap bb to get its pointer before it is nullified.
  (let ((ptr (unwrap-blackbox bb)))
    ;; Nullify bb if it is an armor (rather than bare data)
    (when (armor? bb)
      (nullify-armor! bb))
    ;; Pass the original pointer.
    (FOO_DestroyBlackbox ptr)))