[gdal-dev] About the interaction between VRT and VSIMEM

Even Rouault even.rouault at spatialys.com
Wed Sep 23 14:33:57 PDT 2020

> In your scenario, the C++ object corresponding to rgb has a VSILFILE* opened
> on it. And the C ++ object corresponding to vrt keeps the C++ object
> corresponding to rgb alive, hence this works.

I assume your question was triggered by
which is I believe completely unrelated to /vsimem/

My above statement about lifetime of objects must be altered a bit.
GDALAutoCreateWarpedVRT() must be used carefully regarding lifetime of the source 

Long story short, you can do:

hSrcDS = GDALOpen(...) # non shared mode . refcount = 1
hVRTDS = GDALAutoCreateWarpedVRT(hSrcDS, ...) # refcount(hSrcDS) = 2
GDALClose(hVRTDS) # drop the extra ref to hSrcDS ==> #refcount(hSrcDS) = 1 (but do not 
close it)
GDALClose(hSrcDS) # you must close src after hVRTDS. if you close it before hVRTDS, then 
hVRTDS might try to close it again


hSrcDS = GDALOpenShared(...) # shared mode (full ref counting enabled). refcount(hSrcDS) = 
hVRTDS = GDALAutoCreateWarpedVRT(hSrcDS, ...)  # refcount(hSrcDS) = 2
# you can switch the order of the 2 close calls
GDALClose(hVRTDS) # refCount(hSrcDS) = 1
GDALClose(hSrcDS) # refCount(hSrcDS) = 0 -> really destroyed here

The reason is that in non shared mode GDALClose() ignores ref counting and always deletes 
the dataset. Historical behaviour


Spatialys - Geospatial professional services
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20200923/3b20d799/attachment-0001.html>

More information about the gdal-dev mailing list