Daniel Aarno - 2003-07-11

Hi, all. First of all I must say this is a great idea that will give the C++ language something that have made it terrible to use when compared to other oo languages such as java or obj-c (at least with all the openstep classes).

For me the most important thing Java provides is garbage collection and as such I feel it is important to have such solution in this library. I have two ideas on how to implement this.

The first idea is to create a new type of pointer, let's call them references (not the c++ reference you get by &). You then make a special template class called ref<Template T>. To allocate a an object by reference  you then do a:

ref<MyObject*> o = new MyObject(myArgs);

in this way you can keep track of all references to an object, assuming the programmer only use references to objects and not try to store the address of an object directly. The ref class could then contain a list of all object allocated and a reference count for each allocated object. A collect() method could then be run periodically to see which objects reference count reached zero and then delete them. Possibly the ref count could be in the object, this way you could "force" an object to remain in memory an not be collected by manually retaining (increasing the ref count) the object if such special case is needed (for instance you which to move from reference to pointer).

The other approach would be to implement something like the NSAutoreleasePool of openstep and cocoa. This puts the burden on the programmer to set up and destroy autorelease pools under some circumstances. But surely this is better than nothing, and the main loop as well as the run() method of the thread class could make this automatic.

/Bishop