Skip to content

Conversation

@Fizzixnerd
Copy link

Expose this function as Ctypes.keep_alive, and add documentation to the Ctypes.mli for it. In addition, remove the old use_value completely: from ctypes_roots.c, from cstubs_memory_stubs.ml.

Add Cstubs_internal.keep_alive and change code-gen to use this instead of old use_value.

Expose this function as Ctypes.keep_alive, and add documentation to the
Ctypes.mli for it. In addition, remove the old `use_value` completely:
from ctypes_roots.c, from cstubs_memory_stubs.ml.

Add `Cstubs_internal.keep_alive` and change code-gen to use this
instead of old `use_value`.
@Fizzixnerd
Copy link
Author

Related to: #571

zero-filled. *)

val keep_alive : 'a -> unit
(** Inserting [keep_alive x;] in a sequence of expressions ensures that
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(** Inserting [keep_alive x;] in a sequence of expressions ensures that
(** Inserting [keep_alive x] in a sequence of expressions ensures that


val keep_alive : 'a -> unit
(** Inserting [keep_alive x;] in a sequence of expressions ensures that
the garbage collector will not collect [x] until after that [keep_alive]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the garbage collector will not collect [x] until after that [keep_alive]
the garbage collector will not collect [x] until [keep_alive]

Comment on lines +208 to +217
let strchr = Foreign.foreign "strchr" (ptr char @-> char @-> returning (ptr char)) in
let p = CArray.of_string "abc" in
let q = strchr (CArray.start p) 'a' in
let () = Gc.compact () in
let () = Printf.printf "%c\n" !@q in
keep_alive p;
]}
Without the [keep_alive p] at the bottom, [p] could be collected during the
[Gc.compact ()] call, which would make [q] an invalid pointer into the
now-collected [p], leading to undefined behavior. *)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice example, but I think it would be even stronger without the call to Gc.compact, because most programs don't call Gc functions.

let q = strchr (CArray.start p) 'a' in
let () = Gc.compact () in
let () = Printf.printf "%c\n" !@q in
keep_alive p;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
keep_alive p;
keep_alive p

Comment on lines +135 to +137
(* To avoid a dependence on Ctypes_memory, we redefine keep_alive here. *)
let keep_alive x = ignore (Sys.opaque_identity x) in
Gc.finalise (fun _ -> keep_alive src) ba; ba
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(* To avoid a dependence on Ctypes_memory, we redefine keep_alive here. *)
let keep_alive x = ignore (Sys.opaque_identity x) in
Gc.finalise (fun _ -> keep_alive src) ba; ba
Gc.finalise (fun _ -> ignore (Sys.opaque_identity src)) ba; ba

(lang dune 2.9)
(name ctypes)
(version 0.22.0)
(version 0.23.0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to change the version

let () = Printf.printf "%c\n" !@q in
keep_alive p;
]}
Without the [keep_alive p] at the bottom, [p] could be collected during the
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Without the [keep_alive p] at the bottom, [p] could be collected during the
Without [keep_alive p], [p] could be collected during the

keep_alive p;
]}
Without the [keep_alive p] at the bottom, [p] could be collected during the
[Gc.compact ()] call, which would make [q] an invalid pointer into the
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[Gc.compact ()] call, which would make [q] an invalid pointer into the
[Gc.compact ()] call, making [q] an invalid pointer into the

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants