[Qgis-developer] Avoid segmentation fault on selection when commit

Adrien Gruson agruson at archivideo.com
Fri Jul 11 10:28:27 EDT 2008


Hi all,
I develop Qgis plugin and I have a little problem with selections and 
commit.
_
Steps :_

  1. You select added features (they have negative id).
  2. You commit change (commit attribute positive id to added features)
     but your mSelectedFeatureIds haven't updated and have always
     negative Id in mSelectedFeatureIds.
  3. If the user doesn't reselect features, you have a segmentation
     fault because if you do a iteration on mSelectedFeatures there are
     wrong id.


_A possible solution :

_

   * Erase all negative ids in commitChanges
     
<http://doc.qgis.org/classQgsVectorLayer.html#fab34baf331320a8c3212993f5fccfa1> 

     ()

Advantage : easy to do
Disadvantage: user's selection change when he commit

   * Provider return new id of added feature and replace in
     mSelectedFeatureIds.

Advantage :  no change for user when he commit.
Disadvantage : more change to do in code.

Ps: I attach patch of first solution.
-------------- next part --------------
Index: /raid/homes/agruson/workspace/SVN_Src_Qgis/core/qgsvectorlayer.cpp
===================================================================
--- /raid/homes/agruson/workspace/SVN_Src_Qgis/core/qgsvectorlayer.cpp	(revision 8751)
+++ /raid/homes/agruson/workspace/SVN_Src_Qgis/core/qgsvectorlayer.cpp	(working copy)
@@ -2476,6 +2476,14 @@
     }
     else
     {
+	  //Remove all negative Id in selected features
+	  if(mSelectedFeatureIds.size() > 0)
+	  {
+	    for(QgsFeatureList::iterator it = mAddedFeatures.begin(); it != mAddedFeatures.end(); ++it)
+	    {
+	    	mSelectedFeatureIds.remove(it->featureId());
+	    }
+	  }  
       // done, remove features from the list
       mAddedFeatures.clear();
       addedFeaturesOk = TRUE;


More information about the Qgis-developer mailing list