Index: trunk/SophyaLib/BaseTools/swsegdb.h
===================================================================
--- trunk/SophyaLib/BaseTools/swsegdb.h	(revision 3213)
+++ trunk/SophyaLib/BaseTools/swsegdb.h	(revision 3214)
@@ -84,8 +84,12 @@
   SwSegDataBlock(const SwSegDataBlock<T>& a)
   {
-    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
+    a.mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef = a.mSRef;
     mSRef->nref++;
-    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    size_t segsz = mSRef->segsize;
+    a.mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    buff = new T[segsz];
+    bidx = -1;        
+    fgcstbuff = true; 
   }
 
@@ -113,12 +117,9 @@
   {
     Delete();
-    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef = new SWSDREF;
+    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef->nref = 1;
     mSRef->segsize = segsz;
     mSRef->dsid = AnyDataObj::getUniqueId();
-    mSRef->buff = new T[segsz];
-    mSRef->bidx = -1;        
-    mSRef->fgcstbuff = true; 
     for(size_t k=0; k<nbseg; k++) {
       mSRef->swp.push_back(0);
@@ -127,4 +128,7 @@
     mSRef->swapper = NULL;
     mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    buff = new T[segsz];
+    bidx = -1;        
+    fgcstbuff = true; 
   }
 
@@ -148,21 +152,13 @@
   virtual T* GetSegment(size_t k) 
   {
-    T* rs = NULL;
-    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     getSeg(k);
-    mSRef->fgcstbuff = false;
-    rs = mSRef->buff;
-    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
-    return rs;
+    fgcstbuff = false;
+    return buff;
   }
   //! Return the const (read-only) pointer to data segment \b k         
   virtual T const * GetCstSegment(size_t k) const 
   {
-    T const * rs = NULL;
-    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
-    if (getSeg(k))  mSRef->fgcstbuff = true;
-    rs = mSRef->buff;
-    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
-    return rs;    
+    if (getSeg(k))  fgcstbuff = true;
+    return buff;    
   }
 
@@ -171,8 +167,12 @@
   {
     Delete();
-    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
+    a.mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef = a.mSRef;
     mSRef->nref++;
-    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    size_t segsz = mSRef->segsize;
+    a.mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    buff = new T[segsz];
+    bidx = -1;        
+    fgcstbuff = true; 
     return *this;
   }
@@ -182,11 +182,11 @@
   {
     mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
-    if ((mSRef->bidx >= 0) && !mSRef->fgcstbuff) {
-      int_8 nswp = mSRef->swapper->WriteToSwap(mSRef->buff, mSRef->segsize, mSRef->bidx, 
-					       mSRef->swp[mSRef->bidx], mSRef->fgwp[mSRef->bidx]);
-      mSRef->swp[mSRef->bidx] = nswp;
-      mSRef->fgwp[mSRef->bidx] = true;
-      mSRef->bidx = -1;
-      mSRef->fgcstbuff = true;  
+    if ((bidx >= 0) && !fgcstbuff) {
+      int_8 nswp = mSRef->swapper->WriteToSwap(buff, mSRef->segsize, bidx, 
+					       mSRef->swp[bidx], mSRef->fgwp[bidx]);
+      mSRef->swp[bidx] = nswp;
+      mSRef->fgwp[bidx] = true;
+      bidx = -1;
+      fgcstbuff = true;  
    }
     mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
@@ -211,26 +211,29 @@
     if (mSRef->nref > 0)  { 
       mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+      delete[] buff;
       mSRef = NULL; 
       return; 
     }
     if (mSRef->swapper) delete mSRef->swapper;  
-    delete[] mSRef->buff;
     mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     delete mSRef;
+    delete[] buff;
     mSRef = NULL;  
   } 
   bool getSeg(size_t k) const
   {
-    if (k == mSRef->bidx)  return false;
-    if ((mSRef->bidx >= 0) && !mSRef->fgcstbuff) {
-      int_8 nswp = mSRef->swapper->WriteToSwap(mSRef->buff, mSRef->segsize, mSRef->bidx, 
-					       mSRef->swp[mSRef->bidx], mSRef->fgwp[mSRef->bidx]);
-      mSRef->swp[mSRef->bidx] = nswp;
-      mSRef->fgwp[mSRef->bidx] = true;
+    if (k == bidx)  return false;
+    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
+    if ((bidx >= 0) && !fgcstbuff) {
+      int_8 nswp = mSRef->swapper->WriteToSwap(buff, mSRef->segsize, bidx, 
+					       mSRef->swp[bidx], mSRef->fgwp[bidx]);
+      mSRef->swp[bidx] = nswp;
+      mSRef->fgwp[bidx] = true;
     }
     if (mSRef->fgwp[k]) 
-      mSRef->swapper->ReadFromSwap(k, mSRef->swp[k], mSRef->buff, mSRef->segsize);
+      mSRef->swapper->ReadFromSwap(k, mSRef->swp[k], buff, mSRef->segsize);
     //DEL-02022007    else { delete[] mSRef->buff;  mSRef->buff = new T[mSRef->segsize]; }  
-    mSRef->bidx = k;
+    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    bidx = k;
     return true;
   }
@@ -243,7 +246,4 @@
     mutable std::vector< int_8 > swp;  // swap position tag for each segment
     mutable std::vector< bool > fgwp;  // swap flag (true = already swapped)  for each segment
-    mutable T * buff;                // Data buffer   
-    mutable int_8 bidx;              // segment index (number) corresponding to buffer 
-    mutable bool fgcstbuff;            // true : this is a constant T * buff<
     DataSwapperInterface<T> * swapper;   // Data swapper
     ThSafeOp gThsop;         // Mutex for thread safe operation one / SWSDREF struct 
@@ -251,4 +251,7 @@
   //! \endcond
   SWSDREF *  mSRef;    //!< SWSDREF structure for reference sharing
+  mutable T * buff;                // Data buffer   
+  mutable int_8 bidx;              // segment index (number) corresponding to buffer 
+  mutable bool fgcstbuff;          // true : this is a constant T * buff
 
 };
