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

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

Expand Messages
  • Richard Jones
    May 2, 2006
    • 0 Attachment
      On Tue, May 02, 2006 at 08:56:17AM +0200, Florent Monnier wrote:
      > I would like that a method of an object to be called when this object is
      > finalized. Is it possible ?

      I think the answer is _no_, because calling the method of an object
      assumes that you have a pointer to the object around, and if you have
      a pointer to the object around then the object can never become truly
      unreachable, so it is never finalised :-(

      Here is my test program. Note that the finaliser isn't called, and I
      assume it's because self#finaliser contains a pointer (self) to the

      #!/usr/bin/ocamlrun ocaml

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


      (* 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"

      It might be best to ask about this on caml-list ...


      Richard Jones, CTO Merjis Ltd.
      Merjis - web marketing and technology - http://merjis.com
      Team Notepad - intranets and extranets for business - http://team-notepad.com
    • Show all 7 messages in this topic