Index: trunk/python/pyregfi/__init__.py
===================================================================
--- trunk/python/pyregfi/__init__.py	(revision 213)
+++ trunk/python/pyregfi/__init__.py	(revision 214)
@@ -6,4 +6,5 @@
 
 import sys
+import weakref
 from pyregfi.structures import *
 
@@ -150,18 +151,18 @@
 #  access to base structures  
 class _StructureWrapper(object):
-    hive = None
-    base = None
+    _hive = None
+    _base = None
 
     def __init__(self, hive, base):
-        self.hive = hive
+        self._hive = hive
         # XXX: check for NULL here, throw an exception if so.
-        self.base = base
+        self._base = base
 
     def __del__(self):
-        regfi.regfi_free_record(self.base)
+        regfi.regfi_free_record(self._base)
         hive = None
 
     def __getattr__(self, name):
-        return getattr(self.base.contents, name)
+        return getattr(self._base.contents, name)
 
     def __eq__(self, other):
@@ -188,26 +189,32 @@
 
 class _GenericList(object):
-    hive = None
-    key = None
-    length = None
-    current = None
-
-    # implementation-specific functions
-    fetch_num = None
-    find_element = None
-    get_element = None
-    constructor = None
+    _hive = None
+    _key = None
+    _length = None
+    _current = None
+
+    # implementation-specific functions for _SubkeyList and _ValueList
+    _fetch_num = None
+    _find_element = None
+    _get_element = None
+    _constructor = None
 
     def __init__(self, key):
-        self.hive = key.hive
+        self._hive = key._hive
+
+        # Normally it's good to avoid cyclic references like this 
+        # (key.list.key...) but in this case it makes ctypes memory
+        # management easier to reference the Key instead of the base
+        # structure.  We use a weak reference in order to allow for garbage 
+        # collection, since value/subkey lists should not be usable if their
+        # parent Key is freed anyway.
+
         # XXX: check for NULL here, throw an exception if so.
-        self.key = key
-        self.length = self.fetch_num(key.base)
-    
-    def __del__(self):
-        self.key = None
-
+        self._key = weakref.proxy(key)
+        self._length = self._fetch_num(key._base)
+
+    
     def __len__(self):
-        return self.length
+        return self._length
 
     def __getitem__(self, name):
@@ -219,8 +226,9 @@
             name = create_string_buffer(bytes(name))
 
-        if self.find_element(self.hive.file, self.key.base, name, byref(index)):
-            return self.constructor(self.hive, self.get_element(self.hive.file,
-                                                                self.key.base,
-                                                                index))
+        if self._find_element(self._hive.file, self._key.base, name, byref(index)):
+            return self._constructor(self._hive, 
+                                     self._get_element(self._hive.file,
+                                                       self._key.base,
+                                                       index))
         raise KeyError('')
 
@@ -232,31 +240,30 @@
     
     def __iter__(self):
-        self.current = 0
+        self._current = 0
         return self
     
     def __next__(self):
-        if self.current >= self.length:
+        if self._current >= self._length:
             raise StopIteration('')
 
-        elem = self.get_element(self.hive.file, self.key.base,
-                                c_uint32(self.current))
-        self.current += 1
-        return self.constructor(self.hive, elem)
+        elem = self._get_element(self._hive.file, self._key._base,
+                                c_uint32(self._current))
+        self._current += 1
+        return self._constructor(self._hive, elem)
     
     # For Python 2.x
-    def next(self):
-        return self.__next__()
+    next = __next__
 
 
 class _SubkeyList(_GenericList):
-    fetch_num = regfi.regfi_fetch_num_subkeys
-    find_element = regfi.regfi_find_subkey
-    get_element = regfi.regfi_get_subkey
+    _fetch_num = regfi.regfi_fetch_num_subkeys
+    _find_element = regfi.regfi_find_subkey
+    _get_element = regfi.regfi_get_subkey
 
 
 class _ValueList(_GenericList):
-    fetch_num = regfi.regfi_fetch_num_values
-    find_element = regfi.regfi_find_value
-    get_element = regfi.regfi_get_value
+    _fetch_num = regfi.regfi_fetch_num_values
+    _find_element = regfi.regfi_find_value
+    _get_element = regfi.regfi_get_value
 
 
@@ -287,6 +294,6 @@
 
     def fetch_security(self):
-        return Security(self.hive,
-                        regfi.regfi_fetch_sk(self.hive.file, self.base))
+        return Security(self._hive,
+                        regfi.regfi_fetch_sk(self._hive.file, self.base))
 
 
@@ -300,5 +307,5 @@
         ret_val = None
         if name == "data":
-            data_p = regfi.regfi_fetch_data(self.hive.file, self.base)
+            data_p = regfi.regfi_fetch_data(self._hive.file, self.base)
             try:
                 data_struct = data_p.contents
@@ -330,5 +337,5 @@
         elif name == "data_raw":
             # XXX: should we load the data without interpretation instead?
-            data_p = regfi.regfi_fetch_data(self.hive.file, self.base)
+            data_p = regfi.regfi_fetch_data(self._hive.file, self.base)
             try:
                 data_struct = data_p.contents
@@ -357,6 +364,6 @@
 # Avoids chicken/egg class definitions.
 # Also makes for convenient code reuse in these lists' parent classes.
-_SubkeyList.constructor = Key
-_ValueList.constructor = Value
+_SubkeyList._constructor = Key
+_ValueList._constructor = Value
 
 
@@ -427,5 +434,5 @@
             raise Exception("Could not create iterator.  Current log:\n"
                             + GetLogMessages())
-        self.hive = hive
+        self._hive = hive
         
     def __getattr__(self, name):
@@ -463,6 +470,5 @@
 
     # For Python 2.x
-    def next(self):
-        return self.__next__()
+    next = __next__
 
     def down(self):
@@ -484,5 +490,5 @@
 
     def current_key(self):
-        return Key(self.hive, regfi.regfi_iterator_cur_key(self.iter))
+        return Key(self._hive, regfi.regfi_iterator_cur_key(self.iter))
 
     #XXX Add subkey/value search accessor functions (?)
