<div dir="ltr">Awesome, thanks so much. <div><br></div><div><br></div><div>Cheers, Mike. </div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, May 11, 2025 at 8:29 AM Even Rouault <<a href="mailto:even.rouault@spatialys.com" target="_blank">even.rouault@spatialys.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u>

  
    
  
  <div>
    <p><br>
    </p>
    <div>Le 11/05/2025 à 00:20, Michael Sumner
      via gdal-dev a écrit :<br>
    </div>
    <blockquote type="cite">
      
      <div dir="ltr">
        <div>Hello, I have a question about safely re-casting a dataset
          to another, I want to use Multidim to re-orient an MDArray as
          a classic 2D raster. I was a bit surprised that this works (I
          just overwrite poSrcDS with the classic form): </div>
        <div><br>
        </div>
        <div>I'm passing in 'osFilename', 'osArrayName' and two
          dimension indexes 'index_x' and 'index_y', the first "Open()"
          line is a literal copy from vrtdataset.cpp: <br>
          <br>
          <br>
          // 
          I've removed all the nullptr checks from this code<br>
          auto poSrcDS = std::unique_ptr<GDALDataset,
          GDALDatasetUniquePtrReleaser>(<br>
                   GDALDataset::Open(osFilename, GDAL_OF_RASTER |
          GDAL_OF_VERBOSE_ERROR,<br>
                                    aosAllowedDrivers.List(),
          aosOpenOptions.List(),<br>
                                    nullptr));<br>
          // now open in mdim mode, and get the specified MDArray in
          Classic form with index_x,index_y<br>
          auto poMDimDS =
          std::unique_ptr<GDALDataset>(GDALDataset::Open(osFilename,
          GDAL_OF_MULTIDIM_RASTER));<br>
          auto poMdimGroup = poMDimDS->GetRootGroup();<br>
          auto poArrayDS =
          poMdimGroup->OpenMDArrayFromFullname(osFilename);<br>
          auto poClassicDS = poArrayDS->AsClassicDataset(index_x,
          index_y);<br>
          <br>
          // FIXME how to safely swap poSrcDS with poClassicDS<br>
          poSrcDS = std::unique_ptr<GDALDataset,
          GDALDatasetUniquePtrReleaser>(poClassicDS);</div>
        <div><br>
        </div>
        <div>My question is about the last line where poClassic
          "replaces" poSrcDS: what am I responsible for to make that
          safe? <br>
        </div>
      </div>
    </blockquote>
    <p>That's perfectly fine. In the mdim API, objects returned by
      GetRootGroup(), OpenMDArray(), AsClassicDataset(), etc. , keep the
      necessary parent objects alive through shared_ptr mechanism.  
      Furthermore in your example, there is no relationship between
      poSrcDS et poClassicDS, so even if that wasn't the case, your
      final assignment would be still safe.<br>
    </p>
    <span style="white-space:pre-wrap">
</span>
    <pre cols="72">-- 
<a href="http://www.spatialys.com" target="_blank">http://www.spatialys.com</a>
My software is free, but my time generally not.</pre>
  </div>

</blockquote></div><div><br clear="all"></div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr">Michael Sumner<br>Research Software Engineer<br>Australian Antarctic Division<br>Hobart, Australia<br>e-mail: <a href="mailto:mdsumner@gmail.com" target="_blank">mdsumner@gmail.com</a></div></div>