Fix issue with freeing allocation with circular references.

Change-Id: I45871c20a192815eafee77f95e17a025f6dcf9d1
diff --git a/rsObjectBase.cpp b/rsObjectBase.cpp
index f428f94..f5ced26 100644
--- a/rsObjectBase.cpp
+++ b/rsObjectBase.cpp
@@ -81,6 +81,10 @@
 void ObjectBase::preDestroy() const {
 }
 
+bool ObjectBase::freeChildren() {
+    return false;
+}
+
 bool ObjectBase::checkDelete(const ObjectBase *ref) {
     if (!ref) {
         return false;
@@ -217,6 +221,28 @@
     }
 }
 
+void ObjectBase::freeAllChildren(Context *rsc) {
+    if (rsc->props.mLogObjects) {
+        LOGV("Forcing release of all child objects.");
+    }
+
+    // This operation can be slow, only to be called during context cleanup.
+    ObjectBase * o = (ObjectBase *)rsc->mObjHead;
+    while (o) {
+        if (o->freeChildren()) {
+            // deleted ref to self and possibly others, restart from head.
+            o = (ObjectBase *)rsc->mObjHead;
+        } else {
+            o = (ObjectBase *)o->mNext;
+        }
+    }
+
+    if (rsc->props.mLogObjects) {
+        LOGV("Objects remaining.");
+        dumpAll(rsc);
+    }
+}
+
 void ObjectBase::dumpAll(Context *rsc) {
     asyncLock();