Skip to content

Commit

Permalink
Add dummy finalise to make sure container is not collected before the
Browse files Browse the repository at this point in the history
stream is.
  • Loading branch information
toots committed Mar 16, 2024
1 parent a1794c7 commit ffb9e27
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion av/av.ml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ type ('a, 'b, 'c) stream = {

type media_type = MT_audio | MT_video | MT_data | MT_subtitle

let mk_stream container index = { container; index; decoder = None }
(* Dummy function to prevent the container from being
collected before the stream is. *)
external dummy_cleanup : _ container -> unit = "ocaml_av_dummy_cleanup"

let mk_stream container index =
let finalise () = dummy_cleanup container in
let s = { container; index; decoder = None } in
Gc.finalise_last finalise s;
s

external get_codec_params : (_, 'm, _) stream -> 'm Avcodec.params
= "ocaml_av_get_stream_codec_parameters"
Expand Down
5 changes: 5 additions & 0 deletions av/av_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ CAMLprim value ocaml_av_get_streams(value _av, value _media_type) {
CAMLreturn(list);
}

CAMLprim value ocaml_av_dummy_cleanup(value _av) {
CAMLparam1(_av);
CAMLreturn(Val_unit);
}

CAMLprim value ocaml_av_get_stream_codec_parameters(value _stream) {
CAMLparam1(_stream);
CAMLlocal1(ans);
Expand Down

0 comments on commit ffb9e27

Please sign in to comment.