@@ -265,7 +265,6 @@ void parseDestinationString( QString &destination, QString &providerKey, QString
265265QgsFeatureSink *QgsProcessingUtils::createFeatureSink ( QString &destination, QgsProcessingContext &context, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions )
266266{
267267 QVariantMap options = createOptions;
268- QgsVectorLayer *layer = nullptr ;
269268 if ( !options.contains ( QStringLiteral ( " fileEncoding" ) ) )
270269 {
271270 // no destination encoding specified, use default
@@ -275,7 +274,23 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
275274 if ( destination.isEmpty () || destination.startsWith ( QStringLiteral ( " memory:" ) ) )
276275 {
277276 // memory provider cannot be used with QgsVectorLayerImport - so create layer manually
278- layer = QgsMemoryProviderUtils::createMemoryLayer ( destination, fields, geometryType, crs );
277+ std::unique_ptr< QgsVectorLayer > layer ( QgsMemoryProviderUtils::createMemoryLayer ( destination, fields, geometryType, crs ) );
278+ if ( !layer )
279+ return nullptr ;
280+
281+ if ( !layer->isValid () )
282+ {
283+ return nullptr ;
284+ }
285+
286+ // update destination to layer ID
287+ destination = layer->id ();
288+
289+ // this is a factory, so we need to return a proxy
290+ std::unique_ptr< QgsProxyFeatureSink > sink ( new QgsProxyFeatureSink ( layer->dataProvider () ) );
291+ context.temporaryLayerStore ()->addMapLayer ( layer.release () );
292+
293+ return sink.release ();
279294 }
280295 else
281296 {
@@ -297,33 +312,19 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
297312 else
298313 {
299314 // create empty layer
300- {
301- QgsVectorLayerExporter import ( uri, providerKey, fields, geometryType, crs, false , &options );
302- if ( import .errorCode () )
303- return nullptr ;
304- }
315+ std::unique_ptr< QgsVectorLayerExporter > exporter ( new QgsVectorLayerExporter ( uri, providerKey, fields, geometryType, crs, false , &options ) );
316+ if ( exporter->errorCode () )
317+ return nullptr ;
305318
306319 // use destination string as layer name (eg "postgis:..." )
307- layer = new QgsVectorLayer ( uri, destination, providerKey );
320+ std::unique_ptr< QgsVectorLayer > layer ( new QgsVectorLayer ( uri, destination, providerKey ) );
321+ // update destination to layer ID
322+ destination = layer->id ();
323+ context.temporaryLayerStore ()->addMapLayer ( layer.release () );
324+ return exporter.release ();
308325 }
309326 }
310-
311- if ( !layer )
312- return nullptr ;
313-
314- if ( !layer->isValid () )
315- {
316- delete layer;
317- return nullptr ;
318- }
319-
320- // update destination to layer ID
321- destination = layer->id ();
322-
323- context.temporaryLayerStore ()->addMapLayer ( layer );
324-
325- // this is a factory, so we need to return a proxy
326- return new QgsProxyFeatureSink ( layer->dataProvider () );
327+ return nullptr ;
327328}
328329
329330void QgsProcessingUtils::createFeatureSinkPython ( QgsFeatureSink **sink, QString &destination, QgsProcessingContext &context, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &options )
0 commit comments