Index: /trunk/FACT++/src/EventBuilder.c
===================================================================
--- /trunk/FACT++/src/EventBuilder.c	(revision 15509)
+++ /trunk/FACT++/src/EventBuilder.c	(revision 15510)
@@ -126,4 +126,5 @@
     // We will return this amount of memory
     tgb_inuse += MAX_TOT_MEM;
+    gj.bufTot++;
 
     // No free slot available, allocate a new one
@@ -147,4 +148,7 @@
 void TGB_free(void *mem)
 {
+    if (!mem)
+        return;
+
     // Add the last free slot to the stack
     TGB_entry *entry = (TGB_entry*)malloc(sizeof(TGB_entry));
@@ -565,22 +569,18 @@
 
 
-int
-mBufFree (int i)
-{
-   TGB_free(evtCtrl[i].FADhead);
-
-   evtCtrl[i].fEvent = NULL;
-   evtCtrl[i].FADhead = NULL;
-
-   evtCtrl[i].evNum = evtCtrl[i].nRoi = -1;
-   evtCtrl[i].runNum = 0;
-
-   gj.usdMem = tgb_inuse;
-
-   gj.bufTot--;
-
-   return 0;
-
-} /*-----------------------------------------------------------------*/
+void mBufFree (int i)
+{
+    TGB_free(evtCtrl[i].FADhead);
+
+    evtCtrl[i].fEvent = NULL;
+    evtCtrl[i].FADhead = NULL;
+
+    evtCtrl[i].evNum = evtCtrl[i].nRoi = -1;
+    evtCtrl[i].runNum = 0;
+
+    gj.usdMem = tgb_inuse;
+
+    gj.bufTot--;
+}
 
 uint64_t reportIncomplete(int id, const char *txt)
@@ -1025,5 +1025,5 @@
 
           // We have a valid entry, but no memory has yet been allocated
-          if (idx >= 0 && evtCtrl[idx].FADhead == NULL)
+          if (idx >= 0 && evtCtrl[idx].evtStat==0 && evtCtrl[idx].FADhead==NULL)
           {
               // Try to get memory from the big buffer
@@ -1033,6 +1033,6 @@
                   // If this works properly, this is a hack which can be removed, or
                   // replaced by a signal or dim message
-                  if (rd[i].bufTyp==2)
-                      factPrintf(kError, 882, "malloc failed for event %d (run=%d)", evtCtrl[idx].evNum, evtCtrl[idx].runNum);
+                  if (rd[i].bufTyp==1)
+                      factPrintf(kError, 882, "No free memory left for %d (run=%d)", evtCtrl[idx].evNum, evtCtrl[idx].runNum);
                   rd[i].bufTyp = 2;
                   continue;
@@ -1049,5 +1049,4 @@
 
               gj.rateNew++;
-              gj.bufTot++;
               if (gj.bufTot > gj.maxEvt)
                   gj.maxEvt = gj.bufTot;
@@ -1062,4 +1061,10 @@
           if (idx < -1000)
               continue;
+
+          if (evtCtrl[idx].evtStat==-1 || evtCtrl[idx].evtStat>=90)
+          {
+              factPrintf(kError, 882, "Received data of event %d [%d] (run=%d) has already been advanced (stat=%d)... skipping", evtCtrl[idx].evNum, i, evtCtrl[idx].runNum, evtCtrl[idx].evtStat);
+              continue;
+          }
 
           //we have a valid entry in mBuffer[]; fill it
@@ -1153,5 +1158,5 @@
               // the user has expressed that the old events are obsolste now
               // and the run will be closed anyway
-              if (evtCtrl[k].evtStat>=0 && evtCtrl[k].evtStat<90)
+              if (evtCtrl[k].evtStat>0 && evtCtrl[k].evtStat<90)
               {
                   reportIncomplete(k, "expired");
@@ -1185,5 +1190,5 @@
       {
           // Check the more likely case first: incomplete events
-          if (evtCtrl[k0].evtStat>=0 && evtCtrl[k0].evtStat<100)
+          if (evtCtrl[k0].evtStat>0 && evtCtrl[k0].evtStat<100)
           {
               gj.bufNew++;     //incomplete event in Buffer
@@ -1201,4 +1206,5 @@
               //timeout for incomplete events
               evtCtrl[k0].evtStat = 90;
+
               gj.evtSkip++;
 
@@ -1209,8 +1215,8 @@
           // evtState==0 can happen if the event was initialized (some data received)
           // but the data did not make sense (e.g. inconsistent rois)
-          if (evtCtrl[k0].evtStat==0 || evtCtrl[k0].evtStat == 10000)
+          if (evtCtrl[k0].evtStat==10000)
           {
+              evtCtrl[k0].evtStat = -1;
               mBufFree(k0);   //event written--> free memory
-              evtCtrl[k0].evtStat = -1;
 
               gj.evtWrite++;
@@ -1308,9 +1314,9 @@
            // signals that the event can be deleted. (Note, that there are currently never
            // two threads processing the same event at the same time)
-           if ((evtCtrl[k0].evtStat>0 && evtCtrl[k0].evtStat<90) || evtCtrl[k0].evtStat==10000)
+           if (evtCtrl[k0].evtStat<90 || evtCtrl[k0].evtStat==10000)
            {
+               evtCtrl[k0].evtStat = -1;
                mBufFree(k0);   //event written--> free memory
                evtCtrl_frstPtr = (evtCtrl_frstPtr+1) % MAX_EVT;
-               evtCtrl[k0].evtStat = -1;
            }
 
@@ -1493,4 +1499,5 @@
                    factPrintf(kError, 502, "procEvt: Could not open new file for run %d (idx=%d, evt=%d, runOpen failed)", irun, idx, ievt);
                    runCtrl[idx].fileId = 91;
+                   // FIXME: What happens to evtStat? Shell we really just try again?
                    continue;
                }
@@ -1599,5 +1606,5 @@
 }
 
-void checkAndCloseRun(int j, int irun, int cond, int where)
+void checkAndCloseRun(int j, int cond, int where)
 {
     if (!cond &&
@@ -1630,5 +1637,5 @@
     {
         factPrintf(kInfo, 503, "writeEvt-%d: Closed run %d (reason=%d)",
-                   where, irun, ii);
+                   where, runCtrl[j].runId, ii);
         runCtrl[j].fileId = 93+where*2;
     }
@@ -1673,5 +1680,4 @@
 
            const uint32_t irun = evtCtrl[k0].runNum;
-           const int32_t  ievt = evtCtrl[k0].evNum;
 
            const int idx = evtCtrl[k0].runCtrl_idx;
@@ -1697,5 +1703,5 @@
                    factPrintf(kError, 503, "writeEvt: Writing event for run %d failed (runWrite)", irun);
 
-               checkAndCloseRun(idx, irun, rc<0, 1);
+               checkAndCloseRun(idx, rc<0, 1);
            }
 
@@ -1710,5 +1716,5 @@
                //ETIENNE added the condition at this line. dunno what to do with run 0: skipping it
                const int cond = /*runCtrl[j].lastTime < lastStartedTime &&*/ runCtrl[j].runId == 0;
-               checkAndCloseRun(j, runCtrl[j].runId, cond, 2);
+               checkAndCloseRun(j, cond, 2);
            }
        }
@@ -1731,5 +1737,5 @@
    {
        if (runCtrl[j].fileId == 0)
-           checkAndCloseRun(j, runCtrl[j].runId, 1, 3);
+           checkAndCloseRun(j, 1, 3);
    }
 
