[Qgis-developer] Crash when loading layers in memory

A. Golubev golubev at nordcomp.ru
Tue Jan 26 05:36:19 EST 2010


Hi, devs!
I`m trying to load vector layers into memory for better performance, but 
at some point application crashes.
Here is the simple example:
   QDomDocument* doc = new QDomDocument("qgis");
   doc->setContent( &proj_file );

   mMapCanvas->readProject( *doc );

   QDomNodeList legendLayers = doc->elementsByTagName("legendlayer");
   QDomNodeList nl = doc->elementsByTagName( "maplayer" );
   for (int i = 0; i < legendLayers.count(); i++)
   {
       QDomNode nod = legendLayers.item( i ).firstChild().firstChild();
       QDomElement elem = nod.toElement();
       QString lid = elem.attribute("layerid");

       for (int j = 0; j < nl.count(); j++)
       {
           if (lid == nl.item( j ).firstChild().firstChild().nodeValue())
           {
               QDomNode maplay = nl.item(j);
               QString type = nl.item( j ).toElement().attribute("type");

               if (type != "vector")
               {
                   qDebug("raster layers not supported. Terminating!");
                   return 1;
               }

               QgsVectorLayer* newLayer = new QgsVectorLayer();
               QgsVectorLayer* vl = 0;
               if ( newLayer->readXML( maplay ) )
               {
                   QgsVectorDataProvider * spr = newLayer->dataProvider();
                   QString geomType;
                   QString layerName = newLayer->name();
                   if ( newLayer->geometryType() == QGis::Point )
                       geomType = "Point";
                   if ( newLayer->geometryType() == QGis::Line )
                       geomType = "LineString";
                   if ( newLayer->geometryType() == QGis::Polygon )
                       geomType = "Polygon";
                   if ( newLayer->geometryType() == QGis::UnknownGeometry )
                       qDebug() << "Unknown geometry";

                   vl = new QgsVectorLayer( geomType, layerName, "memory" );

                   QgsVectorDataProvider * pr = vl->dataProvider();
                   QgsFeatureList list;
                   QgsFeature f;
                   newLayer->select( spr->attributeIndexes(), 
QgsRectangle() );

                   while ( newLayer->nextFeature( f ) )
                   {
                       list.append( f );
                   }
                   if ( !pr->addFeatures( list ) )
                   {
                       qDebug() << "features not added";
                   }
                     vl->copySymbologySettings( *newLayer );

                   QgsMapLayer* ml = 
QgsMapLayerRegistry::instance()->addMapLayer( vl );
                   myLayerSet.append(QgsMapCanvasLayer(ml, TRUE));
               } else
               {
                   qDebug( "Unable to load layer." );
               }
               delete newLayer;
           }
       }
   }

   delete doc;

If vl->copySymbologySettings( *newLayer ) commented, then loading 
project successfull, else - application crashes.
Any suggestions?


More information about the Qgis-developer mailing list