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

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

Expand Messages
  • Thierry Martinez
    May 2, 2006
      Answering to the following Florent Monnier's question:
      >> I would like that a method of an object to be called when this object is
      >> finalized. Is it possible ?

      Richard Jones wrote:
      > let () =
      > let obj = object (self)
      > method hello = prerr_endline "hello"
      > method finaliser _ = prerr_endline "in finaliser"
      > initializer
      > prerr_endline "in initializer";
      > Gc.finalise self#finaliser self
      > end in

      I am discovering the Gc module thanks to your message. In the OCaml
      documentation, a warning says that the registered callback has to be
      out of the scope of the considered value, such that its closure
      doesn't capture the value. Your code could be rewritten as:

      let finaliser obj = obj#finaliser

      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

      That seems to work.

      --
      Thierry.
    • Show all 7 messages in this topic