Skip to content

Commit

Permalink
set null pointer in free function
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Feb 7, 2024
1 parent a4757a2 commit acb1c19
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/generate/templates/ocaml/array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module {module_name} = struct
let free ctx ptr =
let is_null = Ctypes.is_null ptr || Ctypes.is_null (!@ptr) in
if not ctx.Context.context_free && not is_null then
ignore (Bindings.futhark_free_{elemtype}_{rank}d ctx.Context.handle (!@ptr))
let () = ignore (Bindings.futhark_free_{elemtype}_{rank}d ctx.Context.handle (!@ptr)) in
ptr <-@ Ctypes.null

let cast x =
coerce (ptr void) (ptr {ocaml_ctype}) (to_voidp x)
Expand Down Expand Up @@ -71,7 +72,7 @@ module {module_name} = struct
let shape = ptr_shape ctx.Context.handle ptr in
{{ ptr = Ctypes.allocate ~finalise:(free ctx) (Ctypes.ptr Ctypes.void) ptr; ctx; shape }}

let free t = free t.ctx t.ptr; t.ptr <-@ Ctypes.null
let free t = free t.ctx t.ptr

let _ = of_ptr
end
Expand Down
5 changes: 3 additions & 2 deletions src/generate/templates/ocaml/opaque.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
let free' ctx ptr =
let is_null = Ctypes.is_null ptr || Ctypes.is_null (!@ptr) in
if not ctx.Context.context_free && not is_null then
ignore (Bindings.{free_fn} ctx.Context.handle (!@ptr))
let () = ignore (Bindings.{free_fn} ctx.Context.handle (!@ptr)) in
ptr <-@ Ctypes.null

let of_ptr ctx ptr =
if is_null ptr then raise (Error NullPtr);
{{ opaque_ptr = allocate ~finalise:(free' ctx) (Ctypes.ptr Ctypes.void) ptr; opaque_ctx = ctx }}

let free t = free' t.opaque_ctx t.opaque_ptr; t.opaque_ptr <-@ Ctypes.null
let free t = free' t.opaque_ctx t.opaque_ptr

let _ = of_ptr

0 comments on commit acb1c19

Please sign in to comment.