[Gc] Newbie Question

Boehm, Hans hans_boehm@hp.com
Mon, 10 Feb 2003 09:26:49 -0800

> -----Original Message-----
> From: Simon Gornall [mailto:simon@gornall.net]
> I'm trying to port across a Java program to C++, to make it run a lot
> faster, and I thought at the same time, using an automatic
> garbage-collection library might solve some problems... 
You might also try compiling your Java application with gcj.  Whether or not that helps, and how it compares to C++ code, will depend on the application.
> Basically I want some of the collection classes, (Vector, Hashtable,
> etc.) and some of the basic classes (String, Integer, etc.) 
> and not much
> else. What is slightly confusing me is the use of explicit cleanup
> functions.
> Assuming I implement Vector or Hashtable, I'm going to end up with
> arrays of collectable classes, and so I'll have to 'new' them with a
> specified cleanup function, yes ?
Only if you need explicit cleanup.  If you just want the memory reclaimed, no.
> So, I looked at the tests/test_cpp.cc (specifically, class D seemed to
> look a reasonable model) but inside CleanIp() I don't see how 
> 'D* self'
> is being freed. Also, I'm not sure what the assert is doing 
> "(int)(long)
> ?".
Cleanup functions don't normally deallocate garbage-collectable memory.  That will be reclaimed later anyway.  These functions are intended to reclaim noncollectable memory or other resources.  Their use should be rare.  Indeed, destructors for collectable objects should be rare, just like Java finalize methods.

The assertion is just checking that the data passed into the cleanup function matches what was used in the constructor.  Thus it checks that both are correctly maintained.  ((int)(long) casts are one way to avoid generating a warning when casting a pointer to an int.  Most compilers warn if you use a direct cast to int on a 64-bit architecture, since information is being discarded.  This has nothing to do with the collector.)