[Kstars-devel] KDE/kdeedu/kstars/kstars/skyobjects

Alexey Khudyakov alexey.skladnoy at gmail.com
Wed Jun 3 17:40:43 CEST 2009


SVN commit 977218 by khudyakov:

Eliminate possible double free problem in SkyObject.

AuxInfo wasn't copied in the copy ctor pointer to it was copied 
instead. So if object was copied AuxInfo wuold be be deleted twice and
program will most likely crash.

This is solved via Qt mechanism for implicit data sharing. 
QSharedDataPointer & QSharedData.

CCMAIL: kstars-devel at kde.org



 M  +9 -4      auxinfo.h  
 M  +8 -9      skyobject.cpp  
 M  +3 -2      skyobject.h  


--- trunk/KDE/kdeedu/kstars/kstars/skyobjects/auxinfo.h #977217:977218
@@ -20,6 +20,7 @@
 
 #include <QString>
 #include <QStringList>
+#include <QSharedData>
 
 /**
  *@struct AuxInfo
@@ -30,11 +31,15 @@
  *@version 1.0
  */
 
-typedef struct AuxInfo {
-    QStringList ImageList, ImageTitle;
-    QStringList InfoList, InfoTitle;
+class AuxInfo : public QSharedData
+{
+public:
+    QStringList ImageList;
+    QStringList ImageTitle;
+    QStringList InfoList;
+    QStringList InfoTitle;
     QString userLog;
-} AuxInfo;
+};
 
 #endif
 
--- trunk/KDE/kdeedu/kstars/kstars/skyobjects/skyobject.cpp #977217:977218
@@ -44,25 +44,27 @@
 SkyObject::SkyObject( int t, dms r, dms d, float m,
                       const QString &n, const QString &n2,
                       const QString &lname )
-        : SkyPoint( r, d) {
+    : SkyPoint( r, d),
+      info()
+{
     setType( t );
     Magnitude = m;
     setName(n);
     setName2(n2);
     setLongName(lname);
-    info = NULL;
 }
 
 SkyObject::SkyObject( int t, double r, double d, float m,
                       const QString &n, const QString &n2,
                       const QString &lname )
-        : SkyPoint( r, d) {
+    : SkyPoint( r, d),
+      info()
+{
     setType( t );
     Magnitude = m;
     setName(n);
     setName2(n2);
     setLongName(lname);
-    info = NULL;
 }
 
 SkyObject* SkyObject::clone() const
@@ -70,10 +72,7 @@
     return new SkyObject(*this);
 }
 
-SkyObject::~SkyObject() {
-    delete info;
-    info = NULL;
-}
+SkyObject::~SkyObject() {}
 
 void SkyObject::showPopupMenu( KSPopupMenu *pmenu, const QPoint &pos ) {
     pmenu->createEmptyMenu( this ); pmenu->popup( pos );
@@ -472,5 +471,5 @@
 AuxInfo *SkyObject::getAuxInfo() {
     if( !info )
         info = new AuxInfo; 
-    return info;
+    return &(*info);
 }
--- trunk/KDE/kdeedu/kstars/kstars/skyobjects/skyobject.h #977217:977218
@@ -20,6 +20,7 @@
 
 #include <QString>
 #include <QStringList>
+#include <QSharedDataPointer>
 
 #include <klocale.h>
 
@@ -323,7 +324,7 @@
      *@short Query whether this SkyObject has a valid AuxInfo structure associated with it.
      *@return true if this SkyObject has a valid AuxInfo structure associated with it, false if not.
      */
-    inline bool hasAuxInfo() { return ( ( info == NULL) ? false : true ); }
+    inline bool hasAuxInfo() { return ! (!info); }
 
     /**
      *@return a reference to a QStringList storing a list of Image URLs associated with this SkyObject
@@ -423,7 +424,7 @@
     QString Name, Name2, LongName;
 
     // Pointer to an auxiliary info structure that stores Image URLs, Info URLs etc.
-    AuxInfo *info;
+    QSharedDataPointer<AuxInfo> info;
 
     // store often used name strings in static variables
     static QString emptyString;


More information about the Kstars-devel mailing list