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

Chris Toney jctoney at gmail.com
Sat Apr 18 13:31:40 PDT 2026


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


More information about the gdal-dev mailing list