@@ -496,15 +496,20 @@ module Expert = struct
496496 ()
497497 ;;
498498
499- (* [add_finalizer_exn ] is the same as [add_finalizer]. However, their types in
499+ (* [add_finalizer_ignore ] is the same as [add_finalizer]. However, their types in
500500 core_gc.mli are different, and the type of [add_finalizer] guarantees that it always
501501 receives a heap block, which ensures that it will not raise, while
502502 [add_finalizer_exn] accepts any type, and so may raise. *)
503+ let add_finalizer_ignore x f =
504+ try Stdlib.Gc. finalise (fun x -> Exn. handle_uncaught_and_exit (fun () -> f x)) x with
505+ | Invalid_argument _ -> ()
506+ ;;
507+
503508 let add_finalizer_exn x f =
504509 try Stdlib.Gc. finalise (fun x -> Exn. handle_uncaught_and_exit (fun () -> f x)) x with
505510 | Invalid_argument _ ->
506- ignore (Heap_block. create x : _ Heap_block.t option );
507- (* If [Heap_block.create ] succeeds then [x] is static data and so
511+ ignore (Heap_block. create_exn x : _ Heap_block.t );
512+ (* If [Heap_block.create_exn ] succeeds then [x] is static data and so
508513 we can simply drop the finaliser. *)
509514 ()
510515 ;;
@@ -519,11 +524,16 @@ module Expert = struct
519524 ()
520525 ;;
521526
527+ let add_finalizer_last_ignore x f =
528+ try Stdlib.Gc. finalise_last (fun () -> Exn. handle_uncaught_and_exit f) x with
529+ | Invalid_argument _ -> ()
530+ ;;
531+
522532 let add_finalizer_last_exn x f =
523533 try Stdlib.Gc. finalise_last (fun () -> Exn. handle_uncaught_and_exit f) x with
524534 | Invalid_argument _ ->
525- ignore (Heap_block. create x : _ Heap_block.t option );
526- (* If [Heap_block.create ] succeeds then [x] is static data and so
535+ ignore (Heap_block. create_exn x : _ Heap_block.t );
536+ (* If [Heap_block.create_exn ] succeeds then [x] is static data and so
527537 we can simply drop the finaliser. *)
528538 ()
529539 ;;
@@ -546,6 +556,11 @@ module Expert = struct
546556 let f = protect_finalizer x f in
547557 add_finalizer_exn x f
548558 ;;
559+
560+ let add_finalizer_ignore x f =
561+ let f = protect_finalizer x f in
562+ add_finalizer_ignore x f
563+ ;;
549564 end
550565
551566 let finalize_release = Stdlib.Gc. finalise_release
0 commit comments