Index: /test/pyregfi-smoketest.py
===================================================================
--- /test/pyregfi-smoketest.py	(revision 223)
+++ /test/pyregfi-smoketest.py	(revision 224)
@@ -201,5 +201,5 @@
 files = []
 for f in sys.argv[1:]:
-    files.append((f, open(f,"r+b")))
+    files.append((f, open(f,"rb")))
 
 
@@ -216,4 +216,5 @@
         print
         sys.stdout.flush()
+    fh.close()
 
 hive = None
Index: /trunk/include/regfi.h
===================================================================
--- /trunk/include/regfi.h	(revision 223)
+++ /trunk/include/regfi.h	(revision 224)
@@ -979,4 +979,22 @@
 
 
+/** Increments reference count on record
+ *
+ * Adds an extra internal reference to specified record, making it necessary to
+ * call regfi_free_record on it an additional time before it is freed.  This is
+ * useful in cases where multiple threads/structures need access to a record, 
+ * without requiring them to be in sync with when it is freed.
+ *
+ * Can be used on REGFI_NK, REGFI_VK, REGFI_SK, REGFI_DATA, and
+ * REGFI_CLASSNAME records.
+ *
+ * @return true on success, false otherwise
+ *
+ * @ingroup regfiBase
+ */
+_EXPORT
+bool regfi_reference_record(const void* record);
+
+
 /** Retrieves number of subkeys referenced by this key.
  *
Index: /trunk/lib/regfi.c
===================================================================
--- /trunk/lib/regfi.c	(revision 223)
+++ /trunk/lib/regfi.c	(revision 224)
@@ -1720,4 +1720,12 @@
 
 
+/******************************************************************************
+ *****************************************************************************/
+bool regfi_reference_record(const void* record)
+{
+  if(talloc_reference(NULL, record) != NULL)
+    return true;
+  return false;
+}
 
 
Index: /trunk/python/pyregfi/__init__.py
===================================================================
--- /trunk/python/pyregfi/__init__.py	(revision 223)
+++ /trunk/python/pyregfi/__init__.py	(revision 224)
@@ -200,7 +200,9 @@
         self._base = base
 
+
     # Memory management for most regfi structures is taken care of here
     def __del__(self):
         regfi.regfi_free_record(self._base)
+
 
     # Any attribute requests not explicitly defined in subclasses gets passed
@@ -208,4 +210,5 @@
     def __getattr__(self, name):
         return getattr(self._base.contents, name)
+
     
     ## Test for equality
@@ -224,4 +227,5 @@
         return (type(self) == type(other)) and (self.offset == other.offset)
 
+
     def __ne__(self, other):
         return (not self.__eq__(other))
@@ -244,5 +248,5 @@
 class _GenericList(object):
     _hive = None
-    _key = None
+    _key_base = None
     _length = None
     _current = None
@@ -255,18 +259,20 @@
 
     def __init__(self, key):
+        if not key:
+            raise Exception("Could not create _GenericList; key is NULL."
+                            + "Current log:\n" + GetLogMessages())
+        
+        if not regfi.regfi_reference_record(key._base):
+            raise Exception("Could not create _GenericList; memory error."
+                            + "Current log:\n" + GetLogMessages())
+        self._key_base = key._base
+        self._length = self._fetch_num(self._key_base)
         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 = weakref.proxy(key)
-        self._length = self._fetch_num(key._base)
-    
-    
+    
+    def __del__(self):
+        regfi.regfi_free_record(self._key_base)
+    
+
     ## Length of list
     def __len__(self):
@@ -286,9 +292,9 @@
             name = create_string_buffer(bytes(name))
 
-        if self._find_element(self._hive.file, self._key._base, 
+        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,
+                                                       self._key_base,
                                                        index))
         raise KeyError('')
@@ -308,5 +314,5 @@
             raise StopIteration('')
 
-        elem = self._get_element(self._hive.file, self._key._base,
+        elem = self._get_element(self._hive.file, self._key_base,
                                  ctypes.c_uint32(self._current))
         self._current += 1
@@ -638,7 +644,9 @@
     def __getattr__(self, name):
         if name == "root":
-            if self._root == None:
-                self._root = Key(self, regfi.regfi_get_rootkey(self.file))
-            return self._root
+            # XXX: This creates reference loops.  Need to cache better inside regfi
+            #if self._root == None:
+            #    self._root = Key(self, regfi.regfi_get_rootkey(self.file))
+            #return self._root
+            return Key(self, regfi.regfi_get_rootkey(self.file))
 
         elif name == "modified":
Index: /trunk/python/pyregfi/structures.py
===================================================================
--- /trunk/python/pyregfi/structures.py	(revision 223)
+++ /trunk/python/pyregfi/structures.py	(revision 224)
@@ -241,4 +241,7 @@
 regfi.regfi_free_record.restype = None
 
+regfi.regfi_reference_record.argtypes = [c_void_p]
+regfi.regfi_reference_record.restype = c_bool
+
 regfi.regfi_fetch_num_subkeys.argtypes = [POINTER(REGFI_NK)]
 regfi.regfi_fetch_num_subkeys.restype = c_uint32
