Loading ...
Sorry, an error occurred while loading the content.

5856Re: "ocaml_beginners"::[] object finalization

Expand Messages
  • Remi Vanicat
    May 2, 2006
    • 0 Attachment
      2006/5/2, Thierry Martinez <thierry.martinez@...>:
      > Richard Jones:
      > > This is my revised version, but I still can't get it to run the
      > > finaliser ...
      > >
      > > #!/usr/bin/ocamlrun ocaml
      > >
      > > let finaliser obj = obj#finaliser
      > >
      > > let () =
      > > let obj = object (self)
      > > method hello = prerr_endline "hello"
      > > method finaliser = prerr_endline "in finaliser"
      > > initializer
      > > prerr_endline "in initializer";
      > > Gc.finalise finaliser self
      > > end in
      > >
      > > obj#hello;
      > >
      > > (* cause the object to become unreachable *)
      > > let obj = () in
      > >
      > > (* call the garbage collector so the object should be finalised *)
      > > Gc.full_major ();
      > >
      > > prerr_endline "finished GC full major cycle"
      >
      > I believe the problem here is that the compiler doesn't notice your
      > object is unreachable when you call Gc.full_major: it is a bit unclear
      > for me, but formaly you have two nested bindings of obj, and the
      > compiler has no clue that your first binding will no longer be used
      > since it still is in the scope

      Exactly. More precisely, when you compare

      let bla = new foo in
      let bla = () in
      bar baz

      to
      let bla = new foo in
      bar baz

      then if you bytecompile it, it is mostly the same as the byte compiler
      does not give back the biding to the GC before the biding is ended.
      If you native compile it, it is mostly the same because ocamlopt to
      optimization to give back the biding whenever it is not needed anymore
      (here it is just after it have been declared).

      So one way or another the "let bla = () in" binding can only serve as
      a documentation programing (we don't need the old binding anymore) or
      as a protective way (we can't use the old binding anymore), but
      neither compiler use it for GC.
    • Show all 7 messages in this topic