[Kst] branches/work/kst/hierarchy/kst/src

Eli Fidler eli at staikos.net
Fri Dec 15 23:56:01 CET 2006


SVN commit 613985 by fidler:

fix a bug in KstDoc::purge() where datasources were getting deleted due
to incorrect reference counting (getUsage() was being called without a
reference-counted pointer)

debugging for KstShared, KstDoc::purge()

comments for getUsage()


 M  +2 -0      libkst/kstobject.cpp  
 M  +2 -1      libkst/kstobject.h  
 M  +13 -2     libkst/kstsharedptr.h  
 M  +46 -7     libkstapp/kstdoc.cpp  


--- branches/work/kst/hierarchy/kst/src/libkst/kstobject.cpp #613984:613985
@@ -85,6 +85,8 @@
 }
 
 
+// Returns count - 2 to account for "this" and the list pointer, therefore
+// you MUST have a reference-counted pointer to call this function
 int KstObject::getUsage() const {
   return _KShared_count() - 1;
 }
--- branches/work/kst/hierarchy/kst/src/libkst/kstobject.h #613984:613985
@@ -196,7 +196,8 @@
     virtual void setTagName(const KstObjectTag& tag);
 
     virtual QString tagLabel() const;
-    // Returns count - 2 to account for "this" and the list pointer
+    // Returns count - 2 to account for "this" and the list pointer, therefore
+    // you MUST have a reference-counted pointer to call this function
     virtual int getUsage() const;
 
     // Returns true if update has already been done
--- branches/work/kst/hierarchy/kst/src/libkst/kstsharedptr.h #613984:613985
@@ -21,6 +21,8 @@
 
 #include <qsemaphore.h>
 
+#include <ksdebug.h>
+
 // NOTE: In order to preserve binary compatibility with plugins, you must
 //       not add, remove, or change member variables or virtual functions.
 //       You must also not remove or change non-virtual functions.
@@ -49,13 +51,22 @@
    /**
     * Increases the reference count by one.
     */
-   void _KShared_ref() const { sem++; }
+   void _KShared_ref() const {
+	   sem++;
+//	   kstdDebug() << "KShared_ref: " << (void*)this << " -> " << _KShared_count() << endl;
+//	   kstdDebug() << kstdBacktrace() << endl;
+   }
 
    /**
     * Releases a reference (decreases the reference count by one).  If
     * the count goes to 0, this object will delete itself.
     */
-   void _KShared_unref() const { sem--;  if (sem.total() == sem.available()) delete this; }
+   void _KShared_unref() const {
+	   sem--;
+//	   kstdDebug() << "KShared_unref: " << (void*)this << " -> " << _KShared_count() << endl;
+//	   kstdDebug() << kstdBacktrace() << endl;
+	   if (sem.total() == sem.available()) delete this;
+   }
 
    /**
     * Return the current number of references held.
--- branches/work/kst/hierarchy/kst/src/libkstapp/kstdoc.cpp #613984:613985
@@ -15,6 +15,8 @@
  *                                                                         *
  ***************************************************************************/
 
+//#define PURGEDEBUG
+
 #include <sys/types.h>
 
 #ifdef HAVE_SYS_STAT_H
@@ -940,6 +942,10 @@
 
 
 void KstDoc::purge() {
+#ifdef PURGEDEBUG
+  kstdDebug() << "Purging unused objects" << endl;
+#endif
+
   QString purging = i18n("Purging unused objects");
   bool modified = false;
   bool again = true;
@@ -961,9 +967,13 @@
     // ASSUMPTION: this only gets called from the data manager!
     KST::dataObjectList.lock().writeLock();
     for (KstDataObjectList::Iterator it = KST::dataObjectList.begin(); it != KST::dataObjectList.end(); ++it) {
-      //kstdDebug() << "OBJECT: " << (*it)->tagName() << " USAGE: " << (*it)->getUsage() << endl;
+#ifdef PURGEDEBUG
+      kstdDebug() << "OBJECT: " << (*it)->tag().displayString() << " USAGE: " << (*it)->getUsage() << endl;
+#endif
       if ((*it)->getUsage() == 0 && !kst_cast<EventMonitorEntry>(*it)) {
-        //kstdDebug() << "    -> REMOVED" << endl;
+#ifdef PURGEDEBUG
+        kstdDebug() << "    -> REMOVED" << endl;
+#endif
         KstDataObjectList::Iterator byebye = it;
         --it;
         KST::dataObjectList.remove(byebye);
@@ -981,9 +991,20 @@
     
     // clear unused vectors that are editable 
     for (KstVectorList::ConstIterator it = vectorList.begin(); it != vectorList.end(); ++it) {
-      //kstdDebug() << "VECTOR: " << (*it)->tagName() << " USAGE: " << (*it)->getUsage() << endl;
+#ifdef PURGEDEBUG
+      kstdDebug() << "VECTOR: " << (*it)->tag().displayString() << " USAGE: " << (*it)->getUsage() << endl;
+      if ((*it)->provider())
+        kstdDebug() << "  provider=" << (*it)->provider()->tag().displayString() << endl;
+//      KstRVectorPtr rvp = kst_cast<KstRVector>(*it);
+//      if (rvp && rvp->_file) {
+//        KstDataSource *file = rvp->_file;
+//        kstdDebug() << "  file=" << file->tag().displayString() << " (" << (void*)(&(*file)) << "): " << file->getUsage() << endl;
+//      }
+#endif
       if ((*it)->getUsage() == 1) {
-        //kstdDebug() << "    -> REMOVED" << endl;
+#ifdef PURGEDEBUG
+        kstdDebug() << "    -> REMOVED" << endl;
+#endif
         KST::vectorList.lock().writeLock();
         KST::vectorList.remove(const_cast<KstVector*>((*it).data()));
         KST::vectorList.lock().unlock();
@@ -1000,8 +1021,20 @@
 
     // clear unused matrices that are editable
     for (KstMatrixList::ConstIterator it = matrixList.begin(); it != matrixList.end(); ++it) {
+#ifdef PURGEDEBUG
+      kstdDebug() << "MATRIX: " << (*it)->tag().displayString() << " USAGE: " << (*it)->getUsage() << endl;
+      if ((*it)->provider())
+        kstdDebug() << "  provider=" << (*it)->provider()->tag().displayString() << endl;
+//      KstRMatrixPtr rmp = kst_cast<KstRMatrix>(*it);
+//      if (rmp && rmp->_file) {
+//        KstDataSource *file = rmp->_file;
+//        kstdDebug() << "  file=" << file->tag().displayString() << " (" << (void*)(&(*file)) << "): " << file->getUsage() << endl;
+//      }
+#endif
       if ((*it)->getUsage() == 1) {
-        //kstdDebug() << "    -> REMOVED" << endl;
+#ifdef PURGEDEBUG
+        kstdDebug() << "    -> REMOVED" << endl;
+#endif
         KST::matrixList.lock().writeLock();
         KST::matrixList.remove(const_cast<KstMatrix*>((*it).data()));
         KST::matrixList.lock().unlock();
@@ -1016,8 +1049,14 @@
   KstDataSourceList dataList;
   KST::dataSourceList.lock().readLock();
   for (KstDataSourceList::ConstIterator it = KST::dataSourceList.begin(); it != KST::dataSourceList.end(); ++it) {
-      if ((*it)->getUsage() == 1) {
-        //kstdDebug() << "    -> REMOVED" << endl;
+      KstDataSourcePtr ds = *it; // MUST use a reference-counted pointer to call getUsage()
+#ifdef PURGEDEBUG
+      kstdDebug() << "DATA SOURCE: " << ds->tag().displayString() << " (" << (void*)ds << ") USAGE: " << ds->getUsage() << endl;
+#endif
+      if (ds->getUsage() == 1) {
+#ifdef PURGEDEBUG
+        kstdDebug() << "    -> REMOVED" << endl;
+#endif
         dataList.append(const_cast<KstDataSource*>((*it).data()));
         modified = true;
       }


More information about the Kst mailing list