From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id AAA02076; Mon, 1 Apr 2002 00:40:49 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA03243 for ; Mon, 1 Apr 2002 00:40:48 +0200 (MET DST) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from mel-rto3.wanadoo.fr (smtp-out-3.wanadoo.fr [193.252.19.233]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g2VMelD24749 for ; Mon, 1 Apr 2002 00:40:47 +0200 (MET DST) Received: from mel-rta3.wanadoo.fr (193.252.19.153) by mel-rto3.wanadoo.fr; 1 Apr 2002 00:40:47 +0200 Received: from debian (80.8.74.231) by mel-rta3.wanadoo.fr; 1 Apr 2002 00:40:34 +0200 Received: from moi by debian with local (Exim 3.35 #1 (Debian)) id 16rn4l-0002nm-00 for ; Sun, 31 Mar 2002 23:41:23 +0200 To: caml-list@inria.fr Subject: Re: [Caml-list] initializers and destructor References: <20020331210924.GA11224@cs.unibo.it> From: Remi VANICAT Date: 31 Mar 2002 23:41:23 +0200 In-Reply-To: <20020331210924.GA11224@cs.unibo.it> Message-ID: <87n0wo5sak.dlv@wanadoo.fr> User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 8bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Stefano Zacchiroli writes: > Hi all, > I know and frequently use "initializer" construct in classes, but I'm > not able to find something like destructor that works exectly like an > initializer but that is invoked just before an object last reference is > lost. > > Is this somewhat difficult to implement in relation with the OCaml GC or > I'm simply missing something? there is the finalise function in the module Gc of the standard library. one can write a destructor using it : # class foo = let f x = print_string "test"; print_newline () in object(s) initializer Gc.finalise f s end;; class foo : object end # new foo;; - : foo = # Gc.full_major ();; test - : unit = () # but you should know that there is some pitfall with finalise : # class foo = object(s) val bar = "test" initializer Gc.finalise (fun x -> print_string bar; print_newline ()) s end;; class foo : object end # new foo;; - : foo = # Gc.full_major ();; - : unit = () # Gc.full_major ();; - : unit = () here, the created object is reachable from the closure of finalisation, so it's reachable, and never garbage collected. You should carefully read it's documentation -- Rémi Vanicat vanicat@labri.u-bordeaux.fr http://dept-info.labri.u-bordeaux.fr/~vanicat ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners