
// for tracking refs/unrefs
class Xyzzy : Object {
    public Xyzzy() {
        stdout.printf("*** Object created\n");
    }
    
    ~Xyzzy() {
        stdout.printf("*** Object destroyed\n");
    }
}

// A generic container that uses another generic container internally
class Container<G> : Object {
    // If list is initialized in the constuctor, this problem goes away
    Gee.ArrayList<G> list = new Gee.ArrayList<G>();
    
    public Container() {
        // uncomment to see problem disappear
        //list = new Gee.ArrayList<G>();
    }
    
    public void add(G item) {
        list.add(item);
    }
    
    public G remove() {
        return list.remove_at(0);
    }
}

void direct() {
    // this works without problem
    Gee.ArrayList<Xyzzy> list = new Gee.ArrayList<Xyzzy>();
    
    stdout.printf("adding directly\n");
    list.add(new Xyzzy());
    
    stdout.printf("removing directly\n");
    list.remove_at(0);
    
    stdout.printf("done w/ direct\n");
}

void container() {
    Container<Xyzzy> container = new Container<Xyzzy>();
    
    stdout.printf("adding through container\n");
    container.add(new Xyzzy());
    
    stdout.printf("removing from container\n");
    container.remove();
    
    stdout.printf("done w/ container\n");
}

void main() {
    direct();
    stdout.printf("\n\n");
    container();
}

