Index: trunk/ppStack/src/ppStackSources.c
===================================================================
--- trunk/ppStack/src/ppStackSources.c	(revision 19210)
+++ trunk/ppStack/src/ppStackSources.c	(revision 19213)
@@ -117,5 +117,11 @@
     psMemSetDeallocator(list, (psFreeFunc)stackSourceListFree);
 
-    list->sources = psMemIncrRefCounter(sources);
+    // Copy the sources onto a new array, so there's no confusion between the inputs and outputs
+    long num = sources->n;              // Number of sources
+    list->sources = psArrayAlloc(num);
+    for (long i = 0; i < num; i++) {
+        list->sources->data[i] = psMemIncrRefCounter(sources->data[i]);
+    }
+
     list->bounds = NULL;
     list->indices = NULL;
@@ -447,2 +453,35 @@
     return psMemIncrRefCounter(firstList->sources);
 }
+
+
+
+void ppStackSourcesPrint(const psArray *sources)
+{
+    static int num = 0;                 // Number of source array
+    psString filename = NULL;           // Name of file
+    psStringAppend(&filename, "sources_%d.reg", num++);
+
+    const char *goodColour = "green";   // Colour for good sources
+    const char *badColour = "red";      // Colour for bad sources
+    float radius = 5;                   // Radius for circle
+
+    FILE *file = fopen(filename, "w");  // File to which to write
+    psFree(filename);
+    for (int i = 0; i < sources->n; i++) {
+        pmSource *source = sources->data[i]; // Source of interest
+        if (!source) {
+            continue;
+        }
+
+        float x, y;                     // Source coordinates
+        coordsFromSource(&x, &y, source);
+
+        bool bad = (source->mode & SOURCE_MASK) || !isfinite(source->psfMag) ||
+            source->psfMag > SOURCE_FAINTEST; // Is this a bad source?
+
+        fprintf(file, "image; circle(%f,%f,%f) # color = %s\n", x, y, radius, bad ? badColour : goodColour);
+    }
+    fclose(file);
+
+    return;
+}
