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

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

Expand Messages
  • Thierry Martinez
    May 2, 2006
      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 (it is just masked by a mere notation
      choice, but in my opinion, your two identifiers' having the same name
      here is just relevant at a syntactic level and is not considered by
      Gc[#]). You could indeed have something like, where it is clear that
      the first "obj" is still reachable, whereas the scope is in the same
      shape than in your code:

      let () =
      let obj = object (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 ();
      );
      obj#hello;

      In my opinion, here is a possibly way to force the unreachability:

      let () =
      begin
      let obj = object (self)
      ...
      end

      obj#hello;
      end
      Gc.full_major ()

      --
      Thierry.

      [#] It is very unclear for me, because it seems that a top-level
      binding of an identifier with the same name of a previously bound
      one masks the latter.
    • Show all 7 messages in this topic