[Gc] linked lists

Francisco tester@mailhouse.ods.org
Sat, 08 Mar 2003 13:07:26 +0000


Dear all,

First, many thanks to all who made possible this tool to be available 
free and as an open-source package. If only I could help a tiny bit ...

I've been trying to convert some C++ code to use GC but due to my lack 
of experience things seem not be going as well as I was expecting.


For instance, for this toy program with a simply linked list:
(I was using gc_cleanup since each node holds a file ptr which needs to 
be closed as the "owner" is finalized)

//-------------------------------------------------------------
(Linux Slackware 8.1 with gcc 3.1.1, GC version: gc6.2alpha3)

$ g++ aa.cpp -oaa -fomit-frame-pointer -lgc
//-------------------------------------------------------------

#define GC_DEBUG 1
#include <gc_cpp.h>
#include <iostream>

using namespace std;

class List : public gc_cleanup {
	public:
		int a;
		List *next;
		
		List(int i):next(0) { a = i; }
		void addNext(List *other) { next = other; }
		
		void printMe() {
			List *xx = this;
			while(xx) {cout << xx->a; xx=xx->next; }
		}

		~List() { cout << "destroy: " << a << "\n"; }
};

List *creator() {
	
	List *yy, *xx, *head;
	
	xx = head = new List(0);
	for (int i=1; i<=1000; i++) {
		yy = new List(i); xx->addNext(yy); xx = yy;
	}
	
	return head;
};

void doit() {

	List *head = creator();
	head->printMe();
}; 	

int main() {

	doit();
	GC_gcollect();
}	

//--------------------------------------------------

But only the head of the list gets to be able to print its finalizing 
message with a warning (is it?):
"GC_register_finalizer_ignore_self called with non-base-pointer 0x805bf50"

However if I don't "connect" the list using the "next" chain of pointers 
all the nodes are collected. (once again with the above warning)

If a double-linked list is tried, a message about a pointer cycle 
(course there are cycles!) is produced and the collecting process gives 
up too after the first node.

Almost for sure I'm missing something but ... well ... I can't find what 
it is!



Many thanks for any help,

-Francisco