I am working on a project based on QGIS API c++ 2.8.2 and qt4.8.
I have two methods in a class, the first create a layer and add a point Here is the code for the creating:
LibCartoError CMyQgsS57::createPointLayer(QString pLayerName, QMap<QString,
QVariant> pFeatureName)
{
QgsVectorLayer * lVectorLayer = new QgsVectorLayer("Point?crs=epsg:4326", pLayerName, "memory");
lVectorLayer->startEditing();
QgsVectorDataProvider * lDataProvider = lVectorLayer->dataProvider();
QMapIterator<QString, QVariant> lAttrib( pFeatureName );
QList<QgsField> lMyAttribField;
while (lAttrib.hasNext()) {
lAttrib.next();
// add attribute
lMyAttribField << QgsField(lAttrib.key(), lAttrib.value().type());
}
lDataProvider->addAttributes( lMyAttribField );
// save attributes
lVectorLayer->commitChanges();
// add to the map
QgsMapLayerRegistry::instance()->addMapLayer( lVectorLayer );
mMapLayer.insert(pLayerName, lVectorLayer);
((QList<QgsMapCanvasLayer>*)myListForPrint)->insert( 0, QgsMapCanvasLayer(lVectorLayer, TRUE) );
// return
lError.error_code = NoError;
lError.error_detail = "La creation de la couche Point " + pLayerName + " est effectuée";
return lError;
}
I have a timer that delete this point if the result of an operation is good, else the point is kept in the map (or canvas).
Here is the code for deleting the layer:
LibCartoError CMyQgsS57::deleteLayer(QString pLayerName)
{
LibCartoError lError;
int lIndexOfLayer;
bool lLayerFound = true;
for (int k = 0; (k < ((QList<QgsMapCanvasLayer>*)myListForPrint)->size() && lLayerFound); k++)
{
if(((QList<QgsMapCanvasLayer>*)myListForPrint)->at(k).layer()->name() == pLayerName) {
lIndexOfLayer = k;
lLayerFound = false;
}
}
if( !lLayerFound ) {
// delete the layer
QgsVectorLayer* lLayertoDelete = NULL;
lLayertoDelete = (QgsVectorLayer*) mMapLayer.value( pLayerName );
QgsMapLayerRegistry::instance()->removeMapLayer( lLayertoDelete->id() );
mMapLayer.remove( pLayerName );
((QList<QgsMapCanvasLayer>*)myListForPrint)->removeAt(lIndexOfLayer);
}
lError.error_code = NoError;
lError.error_detail = "Layer :: " + pLayerName + " is deleted";
return lError;
}
If I launch the system monitor, my application takes 4.4 MB each time the delete and create is invoked. If only the createpoint is invoked the use of memory is stable, but if I create / delete a lot of time I can see the use memory increasing.
Is there something missing in my code?