[gdal-dev] Minimum supported C and C++ standards

Ari Jolma ari.jolma at gmail.com
Mon May 9 00:09:22 PDT 2016


07.05.2016, 20:10, Kurt Schwehr kirjoitti:
>
> This is cart before the horse but... as fast as I can so expect 
> typos.  Now just think of a ~1K long function or method with tons of 
> instances and lots of places to bailout successfully or as failures.  
> We have > 9K free/CPLFree/CPLdelete/CPLDestroys that could be < ~100.

I am definitely for having support for unique/smart pointers to objects. 
In my experience one of the things making code difficult to understand 
(and thus maintain/enhance) is long subroutines. Smart pointers would 
often help a lot in that respect.

Ari

>
> GDALMyBigObj *poInstance = CPLCalloc(sizeof(GDALMyBigObj);
> ...
> if (oops) {
>    CPLDelete(poInstance)
>    return;
> }
> ...
> return;  // kaboom
> ...
> if (oops) {
>    CPLDelete(poInstance)
>    return;
> }
> ...
> if (oops) {
>    CPLDelete(poInstance)
>    return;
> }
>
> CPLDelete(poInstance)
> return;
>
> or worse
>
> GDALMyBigObj *poInstance = CPLCalloc(sizeof(GDALMyBigObj);
> ...
> if (oops) {
>    goto END;
> }
> ...
> // Careful what you do here because you are crossing gotos.
> ...
> if (oops) {
>    goto END;
> }
> ...
> if (oops) {
>    goto END;
> }
> ...
> END:
> CPLDelete(poInstance)
> return;
>
> when you could have...  And yes, getting to this is not trivial.  
> There are multiple things here to discuss:
>
> auto poInstance = std::make_unique<GDALMyBigObj>(arg1, arg2, arg3);
>
> if(oops)
> return;  // Everything cleaned up nice
> ...
>
> if(oops)
> return;  // Everything cleaned up nice
> ...
>
> if(oops)
> return;  // Everything cleaned up nice
> ...
>
> return;  // Woohoo!  Success!
>
> My example of the deleter comes from real code where I don't what a 
> heap checker barfing at me when ever a test fails.  Makes writing and 
> debugging tests insane....
>
> // Apache 2.0 license...
>
> {
>   // ...
>   unique_ptr<OGRFeature> feature(layer->GetNextFeature());
>   OGRGeometry *geometry = feature->GetGeometryRef();
>   ASSERT_NE(nullptr, geometry);
>
>   char *wkt_tmp = nullptr;
>   ASSERT_EQ(OGRERR_NONE, geometry->exportToWkt(&wkt_tmp));
>   std::unique_ptr<char, CplFreeDeleter> wkt(wkt_tmp);
>
>   wkt_tmp = nullptr;
>   ASSERT_STREQ("POINT (-109.27 10.3)", wkt.get());
>
> }
>
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20160509/c5ddd438/attachment.html>


More information about the gdal-dev mailing list