--- mod_mem_cache.1.99.c	Fri Dec 12 12:09:49 2003
+++ mod_mem_cache.c	Fri Dec 12 12:52:08 2003
@@ -352,33 +352,26 @@
             sconf->cache_size -= mobj->m_len;
             obj->cleanup = 1;
         }
-        if (sconf->lock) {
-            apr_thread_mutex_unlock(sconf->lock);
-        }
+    }
+	else if (sconf->lock) {
+    	apr_thread_mutex_lock(sconf->lock);
     }
 
     /* Cleanup the cache object */
 #ifdef USE_ATOMICS
-    if (!apr_atomic_dec32(&obj->refcount)) {
-        if (obj->cleanup) {
-            cleanup_cache_object(obj);
-        }
-    }
+    if (!apr_atomic_dec32(&obj->refcount) && (obj->cleanup)) {
 #else
-    if (sconf->lock) {
-        apr_thread_mutex_lock(sconf->lock);
-    }
     obj->refcount--;
     /* If the object is marked for cleanup and the refcount
      * has dropped to zero, cleanup the object
      */
     if ((obj->cleanup) && (!obj->refcount)) {
+#endif
         cleanup_cache_object(obj);
     }
     if (sconf->lock) {
         apr_thread_mutex_unlock(sconf->lock);
     }
-#endif
     return APR_SUCCESS;
 }
 static apr_status_t cleanup_cache_mem(void *sconfv)
@@ -761,26 +754,18 @@
          * protection of the lock 
          */
         apr_atomic_inc32(&obj->refcount);
+        obj->cleanup = 1;
+        if (!apr_atomic_dec32(&obj->refcount)) {
 #else
+        obj->cleanup = 1;
         if (!obj->refcount) {
-            cleanup_cache_object(obj);
-            obj = NULL;
-        }
 #endif
-        if (obj) {
-            obj->cleanup = 1;
-        }
+            cleanup_cache_object(obj);
+		}
     }
     if (sconf->lock) {
         apr_thread_mutex_unlock(sconf->lock);
     }
-#ifdef USE_ATOMICS
-    if (obj) {
-        if (!apr_atomic_dec32(&obj->refcount)) {
-            cleanup_cache_object(obj);
-        }
-    }
-#endif
     return OK;
 }
 
