Hi,

I read a Steve Maguire book named "Guia Microsoft para o desenvolvimento de programas
sem erros" [ironic not?] and add some ideas for debugging memory allocation in apr_pool.c.


1. Memory not initialized or freed is trash, on debug force this!
2. Use intensive of the function "assert" on debug development.

I wait to be helping.

Thanks in advance.

Ranier Vilela
RC Software

ps. Resubmit, forgot the apr_pools.h!
--- apr_pools-old.c     2003-06-29 22:51:25.000000000 -0300
+++ apr_pools.c 2003-06-29 23:49:16.000000000 -0300
@@ -52,6 +52,7 @@
  * <http://www.apache.org/>.
  */
 
+#include "assert.h"
 #include "apr.h"
 #include "apr_private.h"
 
@@ -1339,6 +1340,12 @@
     pool->stat_alloc++;
     pool->stat_total_alloc++;
 
+
+    /*
+     * Memory not initialized is trash! Force this!
+     */
+    memset(mem, APR_MEMORY_TRASH, size);
+
     return mem;
 }
 
@@ -1347,6 +1354,8 @@
 {
     void *mem;
 
+    assert( pool != NULL && size > 0 );
+
     apr_pool_check_integrity(pool);
 
     mem = pool_alloc(pool, size);
@@ -1363,6 +1372,8 @@
 {
     void *mem;
 
+    assert( pool != NULL && size > 0 );
+
     apr_pool_check_integrity(pool);
 
     mem = pool_alloc(pool, size);
@@ -1385,6 +1396,8 @@
     debug_node_t *node;
     apr_uint32_t index;
 
+    assert( pool != NULL );
+
     /* Destroy the subpools.  The subpools will detach themselves from
      * this pool thus this loop is safe and easy.
      */
@@ -1406,9 +1419,15 @@
     while ((node = pool->nodes) != NULL) {
         pool->nodes = node->next;
 
-        for (index = 0; index < node->index; index++)
+        for (index = 0; index < node->index; index++) {
+            /* Memory freed is trash! Force this! */
+            memset( node->beginp[index], APR_MEMORY_TRASH, (node->endp[index] 
- node->beginp[index]) );
+
             free(node->beginp[index]);
+        }
 
+        /* Memory freed is trash! Force this! */
+        memset( node, APR_MEMORY_TRASH, SIZEOF_DEBUG_NODE_T );
         free(node);
     }
 
@@ -1423,6 +1442,8 @@
     apr_thread_mutex_t *mutex = NULL;
 #endif
 
+    assert( pool != NULL );
+
     apr_pool_check_integrity(pool);
 
 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
@@ -1462,6 +1483,8 @@
 APR_DECLARE(void) apr_pool_destroy_debug(apr_pool_t *pool,
                                          const char *file_line)
 {
+    assert( pool != NULL );
+
     apr_pool_check_integrity(pool);
 
 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
@@ -1493,6 +1516,9 @@
         apr_allocator_destroy(pool->allocator);
     }
 
+    /* Memory freed is trash! Force this! */
+    memset(pool, APR_MEMORY_TRASH, SIZEOF_POOL_T);
+
     /* Free the pool itself */
     free(pool);
 }
@@ -1573,7 +1599,11 @@
          */
         if ((rv = apr_thread_mutex_create(&pool->mutex,
                 APR_THREAD_MUTEX_NESTED, pool)) != APR_SUCCESS) {
+
+            /* Memory freed is trash! Force This! */
+            memset( pool, APR_MEMORY_TRASH, SIZEOF_POOL_T );
             free(pool);
+
             return rv;
         }
 #endif /* APR_HAS_THREADS */
--- apr_pools-old.h     2003-03-11 20:51:53.000000000 -0300
+++ apr_pools.h 2003-06-30 00:21:09.000000000 -0300
@@ -92,6 +92,12 @@
 typedef struct apr_pool_t apr_pool_t;
 
 
+/** Value for memory not initialized or freed */
+#if defined(APR_POOL_DEBUG)
+#define APR_MEMORY_TRASH                             0xCC
+#endif
+
+
 /**
  * Declaration helper macro to construct apr_foo_pool_get()s.
  *

Reply via email to