[gdal-dev] outputting to MEM with the C API for CLI algorithms

Even Rouault even.rouault at spatialys.com
Sat Apr 18 13:42:41 PDT 2026


Chris,

the point was that with a MEM dataset, you need to fetch the dataset 
through the output argument handle, contrary to other file formats, or 
you can potentially open it by name after having finalized the algorithm.

The use of  GDALArgDatasetValueGetDatasetRef() in the example is to 
simplify things a bit, as consumption of the dataset in the example is 
done immediately, before GDALAlgorithmArgRelease(), so no need to play 
with dataset ref counting.

If you need the MEM dataset liftetime to exceed the one of the 
algorithm, then GDALArgDatasetValueGetDatasetIncreaseRefCount() paired 
with GDALReleaseDataset() is definitely the way to go.

Even

Le 18/04/2026 à 22:31, Chris Toney via gdal-dev a écrit :
> Hi,
> In "How to use gdal CLI algorithms from C"
> (https://gdal.org/en/latest/programs/gdal_cli_from_c.html):
>
>> When outputting to a MEM -- In Memory datasets dataset, you typically want to get the output dataset with:
>> GDALAlgorithmArgH hArg = GDALAlgorithmGetArg(hAlg, "output");
>> GDALArgDatasetValueH hVal = GDALAlgorithmArgGetAsDatasetValue(hArg);
>> GDALDatasetH hDS = GDALArgDatasetValueGetDatasetRef(hVal);
> But if we use GDALArgDatasetValueGetDatasetRef(hValue):
>
>> This does not modify the reference counter, hence the lifetime of the returned object is not guaranteed to exceed the one of hValue.
> Is there anything wrong with the following, if we want the lifetime of
> the returned object to exceed that of hValue?
>
> GDALDatasetH hDS = nullptr;
> hDS = GDALArgDatasetValueGetDatasetIncreaseRefCount(hArgDSValue);
> if (!hDS) {
>      GDALArgDatasetValueRelease(hArgDSValue);
>      // ...
> }
> // ...
> GDALArgDatasetValueRelease(hArgDSValue);
>
> and then continue using the MEM hDS?
>
> That's been working fine, but I'm wondering why the specific note in
> the tutorial about outputting to MEM in that way?
>
> Thanks.
>
> Chris
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev

-- 
http://www.spatialys.com
My software is free, but my time generally not.
Highly recommend OxiGDAL if you want to live in the 21th century and cure Bixonimania



More information about the gdal-dev mailing list