zhupan / serf

Automatically exported from code.google.com/p/serf
Apache License 2.0
0 stars 0 forks source link

[PATCH] Destroy created APR allocator. #79

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Serf has code in allocator.c to conditionally create an APR allocator 
(apr_allocator_create) when inheriting an existing one doesn't pan out.  
However, it doesn't also have code to destroy that allocator when we're done 
with it.

In issue 76, a user contributed a patch to remedy this.  I've posted a modified 
version thereof here in a separate issues so it doesn't get overlooked.  (My 
version avoids explicitly setting a value already zeroed out via apr_calloc(), 
performs a more general boolean conditional test, and adds a code comment.)

{{{
* buckets/allocator.c

  (serf_bucket_alloc_t): Add 'own_allocator' boolean flag.
  (allocator_cleanup): If we created the associated apr_allocator_t,
    destroy it here.
  (serf_bucket_allocator_create): If we created an apr_allocator_t,
    remember that fact by setting the new 'own_allocator' flag.

Patch by: morlov...@google.com
          cmpilato@red-bean.com
}}}

Index: buckets/allocator.c
===================================================================
--- buckets/allocator.c (revision 1474)
+++ buckets/allocator.c (working copy)
@@ -83,6 +83,7 @@
 struct serf_bucket_alloc_t {
     apr_pool_t *pool;
     apr_allocator_t *allocator;
+    int own_allocator;

     serf_unfreed_func_t unfreed;
     void *unfreed_baton;
@@ -106,6 +107,10 @@
     if (allocator->blocks) {
         apr_allocator_free(allocator->allocator, allocator->blocks);
     }
+    /* If we allocated our own allocator (?!), destroy it here. */
+    if (allocator->own_allocator) {
+        apr_allocator_destroy(allocator->allocator);
+    }

     return APR_SUCCESS;
 }
@@ -123,6 +128,7 @@
         /* This most likely means pools are running in debug mode, create our
          * own allocator to deal with memory ourselves */
         apr_allocator_create(&allocator->allocator);
+        allocator->own_allocator = 1;
     }
     allocator->unfreed = unfreed;
     allocator->unfreed_baton = unfreed_baton;

Original issue reported on code.google.com by cmpilato on 21 Jun 2011 at 4:12

GoogleCodeExporter commented 9 years ago
Fixed in r1534 (aka patch applied). Thanks!

Original comment by gstein on 13 Jul 2011 at 10:53