Bojan Smojver
Fri, 18 Jul 2008 16:00:08 -0700
On Fri, 2008-07-18 at 12:34 +0200, Mladen Turk wrote: > For example apr_memcache.c leeks memory because > it doesn't call apr_pool_destroy in destructor > although it creates one in constructor. > (Probably because with standard cleanup it cores) Does this solve it? -- Bojan
Index: memcache/apr_memcache.c
===================================================================
--- memcache/apr_memcache.c (revision 677819)
+++ memcache/apr_memcache.c (working copy)
@@ -295,7 +295,15 @@
return rv;
}
+static apr_status_t conn_clean(void *data)
+{
+ apr_memcache_conn_t *conn = data;
+ conn->p = NULL; /* so that destructor does not destroy the pool again */
+
+ return APR_SUCCESS;
+}
+
static apr_status_t
mc_conn_construct(void **conn_, void *params, apr_pool_t *pool)
{
@@ -310,7 +318,7 @@
return rv;
}
- conn = apr_palloc(np, sizeof( apr_memcache_conn_t ));
+ conn = malloc(sizeof( apr_memcache_conn_t )); /* non-pool space! */
conn->p = np;
@@ -334,10 +342,13 @@
rv = conn_connect(conn);
if (rv != APR_SUCCESS) {
apr_pool_destroy(np);
+ free(conn);
}
else {
*conn_ = conn;
}
+
+ apr_pool_cleanup_register(np, conn, conn_clean, apr_pool_cleanup_null);
return rv;
}
@@ -359,6 +370,12 @@
/* Return values not checked, since we just want to make it go away. */
apr_socket_sendv(conn->sock, vec, 2, &written);
apr_socket_close(conn->sock);
+
+ if (conn->p) {
+ apr_pool_destroy(conn->p);
+ }
+
+ free(conn); /* free non-pool space */
return APR_SUCCESS;
}