[gdal-dev] help please with close/reopen and GDALSubdatasetInfo

Michael Sumner mdsumner at gmail.com
Sun Dec 3 11:33:13 PST 2023


Awesome, thank you!  I was a bit lost - learnt a lot from exploring this
though.

Cheers, Mike



On Mon, Dec 4, 2023 at 12:11 AM Even Rouault <even.rouault at spatialys.com>
wrote:

> Hi Michael,
>
> It was most missing a "break;" statement when the subdataset of interest
> has been found to exit the loop. Without the break, the loop will
> continue to read papszSubdatasets[] which has been invalidated by the
> poSrcDS->ReleaseRef(). Ah, memory unsafe languages :-). You also had a
> few memory leaks.
>
> Fixed version:
>
> #include "gdal.h"
> #include "gdal_priv.h"
> #include <iostream>
> int main(int argc, char **argv) {
>    GDALAllRegister();
>
>    auto poSrcDS =
>      GDALDataset::Open(argv[1], GDAL_OF_RASTER, nullptr, nullptr, nullptr);
>    if (poSrcDS == nullptr)
>    {
>      return 0;
>    }
>    char **papszSubdatasets = poSrcDS->GetMetadata("SUBDATASETS");
>    int nSubdatasets = CSLCount(papszSubdatasets);
>    if (nSubdatasets > 0)
>    {
>      for (int j = 0; j < nSubdatasets; j += 2)
>      {
>        char* pszSubdatasetSource = CPLStrdup(strstr(papszSubdatasets[j],
> "=") + 1);
>        GDALSubdatasetInfoH info =
> GDALGetSubdatasetInfo(pszSubdatasetSource);
>        char* component = info ?
> GDALSubdatasetInfoGetSubdatasetComponent(info) : NULL;
>        const bool bFound = component && EQUAL(argv[2], component);
>        CPLFree(component);
>        GDALDestroySubdatasetInfo(info);
>        if ( bFound) {
>          std::cout << pszSubdatasetSource << "\n";
>          poSrcDS->ReleaseRef();
>          poSrcDS = GDALDataset::Open(pszSubdatasetSource,
> GDAL_OF_RASTER, nullptr, nullptr, nullptr);
>          CPLFree(pszSubdatasetSource);
>          break;
>        }
>        else {
>          CPLFree(pszSubdatasetSource);
>        }
>      }
>    }
>
>    poSrcDS->ReleaseRef();
>    return 1;
> }
>
> Even
>
>
> Le 03/12/2023 à 05:10, Michael Sumner via gdal-dev a écrit :
> > #include "gdal.h"
> > #include "gdal_priv.h"
> > #include <iostream>
> > int main(int argc, char **argv) {
> >   GDALAllRegister();
> >
> >   auto poSrcDS =
> >     GDALDataset::Open(argv[1], GDAL_OF_RASTER, nullptr, nullptr,
> nullptr);
> >   if (poSrcDS == nullptr)
> >   {
> >     return 0;
> >   }
> >   char **papszSubdatasets = poSrcDS->GetMetadata("SUBDATASETS");
> >   int nSubdatasets = CSLCount(papszSubdatasets);
> >   char *pszSubdatasetSource = nullptr;
> >   if (nSubdatasets > 0)
> >   {
> >     for (int j = 0; j < nSubdatasets; j += 2)
> >     {
> >       pszSubdatasetSource = CPLStrdup(strstr(papszSubdatasets[j], "=")
> > + 1);
> >       GDALSubdatasetInfoH info =
> > GDALGetSubdatasetInfo(pszSubdatasetSource);
> >       if ( EQUAL(argv[2],
> > GDALSubdatasetInfoGetSubdatasetComponent(info))) {
> >         std::cout << pszSubdatasetSource << "\n";
> >         poSrcDS->ReleaseRef();
> >         poSrcDS = GDALDataset::Open(pszSubdatasetSource,
> > GDAL_OF_RASTER, nullptr, nullptr, nullptr);
> >         CPLFree(pszSubdatasetSource);
> >         GDALDestroySubdatasetInfo(info);
> >
> >       }
> >     }
> >   }
> >
> >   poSrcDS->ReleaseRef();
> >   return 1;
> > }
>
> --
> http://www.spatialys.com
> My software is free, but my time generally not.
>
>

-- 
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: mdsumner at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20231204/a1ee9614/attachment-0001.htm>


More information about the gdal-dev mailing list