Index: /branches/jch-memory/psLib/src/sys/psMemory.c
===================================================================
--- /branches/jch-memory/psLib/src/sys/psMemory.c	(revision 10906)
+++ /branches/jch-memory/psLib/src/sys/psMemory.c	(revision 10907)
@@ -9,6 +9,6 @@
 *  @author Joshua Hoblitt, University of Hawaii
 *
-*  @version $Revision: 1.88.2.14 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2007-01-05 01:15:33 $
+*  @version $Revision: 1.88.2.15 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2007-01-05 01:56:34 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -58,5 +58,5 @@
 fprintf(stderr, "%s (%s:%d) ", func, filename, lineno); \
 fprintf(stderr, __VA_ARGS__);\
-psErrorStackPrint(stderr, "\nAborting.  Error stack:"); \
+psErrorStackPrint(stderr, "\nAborting.  Error stack:\n"); \
 fprintf(stderr, "\n");\
 abort();
@@ -74,6 +74,5 @@
 #define HANDLE_BAD_BLOCK(memBlock, file, lineo, func) \
 if (badMemBlock(stderr, memBlock, file, lineo, func)) { \
-    psMemBlockPrint(stderr, memBlock); \
-    PS_MEM_ABORT(__func__, "Unsafe to Continue"); \
+    PS_MEM_ABORT(__func__, "Unsafe to Continue\n"); \
 }
 
@@ -155,28 +154,68 @@
     // p_psAlloc/p_psFree here.
 
-    // ->nextBlock, & -> prevousBlock should not be looked at but this function
-    // as they make be changed out from underneath us by new memory allocation
+    bool bad = false;
+    bool blockPrinted = false;
+
     if (memBlock == NULL) {
-        fprintf(output, _("NULL memory block. Caught in %s at (%s:%d.)."), func, file, lineno);
+        fprintf(output, _("NULL memory block.\n"
+                          "Caught in %s at (%s:%d.).\n"), func, file, lineno);
+        // return now as we can't do anything else
         return true;
     }
 
-    if (memBlock->refCounter == 0) {
+    if (memBlock->refCounter < 1) {
         // using an unreferenced block of memory, are you?
-        fprintf(output, _("Memory block was freed but still being used.  Caught in %s at (%s:%d)."), func, file, lineno);
-        return true;
+        psMemBlockPrint(output, memBlock);
+        blockPrinted = true;
+        fprintf(output, _("\n\tMemory block was freed but still being used.\n"));
+        bad = true;
     }
 
     if (memBlock->startblock != P_PS_MEMMAGIC || memBlock->endblock != P_PS_MEMMAGIC) {
-        fprintf(output, _("Memory block is corrupted; buffer underflow detected.Caught in %s at (%s:%d)."), func, file, lineno);
-        return true;
-    }
+        if (!blockPrinted) {
+            psMemBlockPrint(output, memBlock);
+            blockPrinted = true;
+        }
+        fprintf(output, _("\n\tMemory block is corrupted; buffer underflow detected.\n"));
+        bad = true;
+    }
+
     if (*(psPtr *)((int8_t *) (memBlock + 1) + memBlock->userMemorySize) != P_PS_MEMMAGIC) {
-        fprintf(output,
-                _("Memory block is corrupted; buffer overflow detected. Caught in %s at (%s:%d)."), func, file, lineno);
-        return true;
-    }
-
-    return false;
+        if (!blockPrinted) {
+            psMemBlockPrint(output, memBlock);
+            blockPrinted = true;
+        }
+        fprintf(output, _("\n\tMemory block is corrupted; buffer overflow detected.\n"));
+        bad = true;
+    }
+
+    //  XXX ->nextBlock, & -> prevousBlock really should not be looked at by
+    //  this function as they may be changed out from underneath us by new
+    //  memory allocation.  However, the memBlock itself shouldn't go away (end
+    //  users responsiblity) so all we're really risking is a garbage value.
+
+    if (memBlock == memBlock->nextBlock) {
+        if (!blockPrinted) {
+            psMemBlockPrint(output, memBlock);
+            blockPrinted = true;
+        }
+        fprintf(output, _("\n\tMemory block's ->nextBlock pointer refers to itself.\n"));
+        bad = true;
+    }
+
+    if (memBlock == memBlock->previousBlock) {
+        if (!blockPrinted) {
+            psMemBlockPrint(output, memBlock);
+            blockPrinted = true;
+        }
+        fprintf(output, _("\n\tMemory block's ->previousBlock pointer refers to itself.\n"));
+        bad = true;
+    }
+
+    if (bad) {
+        fprintf(output, _("\tCaught in %s at (%s:%d).\n"), func, file, lineno);
+    }
+
+    return bad;
 }
 
@@ -323,6 +362,4 @@
         if (badMemBlock(output, memBlock, __FILE__, __LINE__, __func__)) {
             nbad++;
-
-            psMemBlockPrint(output, memBlock);
 
             if (abort_on_error) {
@@ -458,6 +495,6 @@
         if (memBlock == NULL) {
             MUTEX_UNLOCK(&memBlockListMutex);
+            psMemBlockPrint(stderr,  ((psMemBlock *)ptr) - 1);
             fprintf(stderr, "Problem reallocating block\n");
-            psMemBlockPrint(stderr,  ((psMemBlock *)ptr) - 1);
             PS_MEM_ABORT(__func__, "Failed to reallocate to %zd bytes at %s (%s:%d)", size, func, file, lineno);
         }
@@ -1076,5 +1113,5 @@
 {
     return fprintf(output,
-                   "Memory Block ID: %lu\n"
+                   "Memory Block ID: %lu @ %p\n"
                    "\tPrevious Block: %p Next Block: %p\n"
                    "\tFree function: %p\n"
@@ -1083,9 +1120,10 @@
                    "\tAllocated in %s at (%s:%d)\n"
                    "\t\tby Thread ID %lu\n",
-                   memBlock->id,
+                   memBlock->id, memBlock,
                    memBlock->previousBlock, memBlock->nextBlock,
                    memBlock->freeFunc,
                    memBlock->userMemorySize, memBlock->refCounter, (memBlock->persistent ? "Yes" : "No"),
-                   memBlock->startblock, memBlock->endblock, (memBlock + 1 + memBlock->userMemorySize),
+                   memBlock->startblock, memBlock->endblock,
+                   *(psPtr *)((int8_t *) (memBlock + 1) + memBlock->userMemorySize),
                    memBlock->func, memBlock->file, memBlock->lineno, memBlock->tid);
 }
