caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Garbage collecting _everything_
@ 2004-06-05 15:36 Roberto Bagnara
  2004-06-05 16:03 ` Daniel Bünzli
  2004-06-05 16:29 ` skaller
  0 siblings, 2 replies; 6+ messages in thread
From: Roberto Bagnara @ 2004-06-05 15:36 UTC (permalink / raw)
  To: caml-list


Dear all,

we have written a small OCaml program that interfaces
to some C++ code.  For all the classes we have interfaced,
we have defined the required `struct custom_operations' that
is then registered with `register_custom_operations()'
and referred to in calls to `alloc_custom()'.  The finalization
functions invoke `delete' on the pointer embedded into the
OCaml value.  We do this with 6 different C++ classes that
are all handled in exactly the same way.

Then we have a small test program written in OCaml whose
last line is

   at_exit Gc.full_major;;

and for which valgrind (http://valgrind.kde.org/) says
we have memory leaks.  We have then verified that the
finalization functions for 2 of the created C++ objects
are never called.

As we are really beginners at OCaml programming, we are
sure we are missing something, but we don't know what.
Isn't `at_exit Gc.full_major;;' supposed to collect
_everything_ at program termination time?
Thanks in advane for your advice,

      Roberto

-- 
Prof. Roberto Bagnara
Computer Science Group
Department of Mathematics, University of Parma, Italy
http://www.cs.unipr.it/~bagnara/
mailto:bagnara@cs.unipr.it

-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Garbage collecting _everything_
  2004-06-05 15:36 [Caml-list] Garbage collecting _everything_ Roberto Bagnara
@ 2004-06-05 16:03 ` Daniel Bünzli
  2004-06-05 16:11   ` Daniel Bünzli
  2004-06-05 16:29 ` skaller
  1 sibling, 1 reply; 6+ messages in thread
From: Daniel Bünzli @ 2004-06-05 16:03 UTC (permalink / raw)
  To: Roberto Bagnara; +Cc: caml-list

Suppose you have this definition in a module

let x = (1.0, 2.0)

This tuple will never be garbage collected, since you can refer to it 
at any time in code via the name x.

Daniel

-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Garbage collecting _everything_
  2004-06-05 16:03 ` Daniel Bünzli
@ 2004-06-05 16:11   ` Daniel Bünzli
  0 siblings, 0 replies; 6+ messages in thread
From: Daniel Bünzli @ 2004-06-05 16:11 UTC (permalink / raw)
  To: Roberto Bagnara; +Cc: caml-list

Maybe I wasn't clear enough, sorry.

> Suppose you have this definition in a module
>
> let x = (1.0, 2.0)

and this one

let f () = x

You cannot garbage collect x since at any time function f may be 
called. For example

  at_exit (fun () -> Gc.full_major; ignore (f ())) ;;

Daniel

-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Garbage collecting _everything_
  2004-06-05 15:36 [Caml-list] Garbage collecting _everything_ Roberto Bagnara
  2004-06-05 16:03 ` Daniel Bünzli
@ 2004-06-05 16:29 ` skaller
  2004-06-05 16:50   ` Richard Jones
  2004-06-05 19:46   ` Roberto Bagnara
  1 sibling, 2 replies; 6+ messages in thread
From: skaller @ 2004-06-05 16:29 UTC (permalink / raw)
  To: Roberto Bagnara; +Cc: caml-list

On Sun, 2004-06-06 at 01:36, Roberto Bagnara wrote:

> Isn't `at_exit Gc.full_major;;' supposed to collect
> _everything_ at program termination time?

Here is what it says about at_exit:

Register the given function to be called at program termination time.
The functions registered with at_exit will be called when the program
executes Pervasives.exit, or terminates, either normally or because of
an uncaught exception. The functions are called in ``last in, first
out'' order: the function most recently added with at_exit is called
first.

Now, since these functions are *arbitrary*, performing a collection
at some particular time will do just the same as it always does:
collect unreachable objects.

Clearly, global variables created thusly:

let x = create_object () 
;;

may be visible to any function defined in the same scope
subsequently.

It is likely such variables must be considered
visible to all functions. In other words, they all must
be roots. The only time these objects could possibly
be released is after all user code has executed,
and that would be after all 'at_exit' functions have
completed.

So I am guessing that you made the mistake of
placing two of your C++ objects in Ocaml global variables.

Make sure the bindings for you test objects are
to variables local to a test function which terminates
and leaves its locals unreachable. You can simply call
Gc.full_major after invoking the test function.

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Garbage collecting _everything_
  2004-06-05 16:29 ` skaller
@ 2004-06-05 16:50   ` Richard Jones
  2004-06-05 19:46   ` Roberto Bagnara
  1 sibling, 0 replies; 6+ messages in thread
From: Richard Jones @ 2004-06-05 16:50 UTC (permalink / raw)
  Cc: caml-list

On Sun, Jun 06, 2004 at 02:29:36AM +1000, skaller wrote:
> Clearly, global variables created thusly:
> 
> let x = create_object () 
[...]

This is interesting.  Is there a way to force OCaml to collect all
objects at exit, even global ones, obviously after all possible user
code has stopped running?  In Java I seem to remember there was a way
to configure the Gc to run all finalizers at exit.  One would think it
should be possible with a setting in OCAMLRUNPARAM.

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
C2LIB is a library of basic Perl/STL-like types for C. Vectors, hashes,
trees, string funcs, pool allocator: http://www.annexia.org/freeware/c2lib/

-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Garbage collecting _everything_
  2004-06-05 16:29 ` skaller
  2004-06-05 16:50   ` Richard Jones
@ 2004-06-05 19:46   ` Roberto Bagnara
  1 sibling, 0 replies; 6+ messages in thread
From: Roberto Bagnara @ 2004-06-05 19:46 UTC (permalink / raw)
  To: skaller; +Cc: caml-list

skaller wrote:
> So I am guessing that you made the mistake of
> placing two of your C++ objects in Ocaml global variables.

That was it, indeed.
Thanks to everybody!
All the best,

     Roberto

-- 
Prof. Roberto Bagnara
Computer Science Group
Department of Mathematics, University of Parma, Italy
http://www.cs.unipr.it/~bagnara/
mailto:bagnara@cs.unipr.it

-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-06-05 19:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-05 15:36 [Caml-list] Garbage collecting _everything_ Roberto Bagnara
2004-06-05 16:03 ` Daniel Bünzli
2004-06-05 16:11   ` Daniel Bünzli
2004-06-05 16:29 ` skaller
2004-06-05 16:50   ` Richard Jones
2004-06-05 19:46   ` Roberto Bagnara

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).