GC, the simple solution

Larry Evans cppljevans at cos-internet.com
Mon Jun 5 14:27:23 PDT 2006


On 06/04/2006 05:40 AM, Daniel Keep wrote:

 > "GC, the simple solution"

[snip]

 > * inability to free cycles
 > * inability to even *run* destructors where cycles are involved, because
 > there is no order in which they can logically be executed and still 
be valid


I think someone else in the thread mentioned this as a programmer error.
However, assuming the programmer intended this, then this programmer
must expect that the MS GC would break the cycle somewhere before
calling the destructors and he must not care where the cycle was
broken, because otherwise he would have used a weak pointer
to close the cycle where he wanted the break to occur.

Anyway, code for doing this GC breaking of cycles in RC
is at:

   http://tinyurl.com/reuzl

See code after comment:

  *  Breaks all arcs in cycle_arcs.

As mentioned in the code comments, this is Christopher's
method for collecting cycles and suffers a delay since it
keeps all smart ptrs in a list and processes the entire list.
Other methods don't keep this list but do a local mark-sweep
at each rc decrement with obvious time penalties.  A compromise
just uses a queue which, when it reaches a certain size,
plays the same role as Christopher's list of all smart pointers.

 >
 > Those last two are the main problem with reference counting, along with
 > the others and slightly added complexity in the compiler.
 >
 > Python, however, does not suffer the cycle problem.  Know why?
 >
 > Because they have a *mark & sweep* garbage collector to clean up the 
cycles.

Then it must not automatically call destructors since then it would
suffer the 2nd problem above.

 >
[snip]

 > As it stands, you can implement reference counting yourself: you just
 > need to write an allocator, and call the incref and decref functions
 > manually.  However, I do think that having built-in *support* for
 > Reference counting would be fantastic.  The less that stuff like this is
 > left in the hands of the programmer, the better.


Smart pointers would be a great help here, but since
D doesn't allow user definition of the assignment operator:

   http://www.digitalmars.com/d/archives/digitalmars/D/learn/984.html

that's not easy.  I'm a newbie, so what's the best way to implement
something like a smart pointer without having to manually:

   increment rc1;
   decrement rc2;
   assign the pointer to its target location;



More information about the Digitalmars-d mailing list