[gdal-dev] Use of std::unique_ptr and auto in new code

Alan Thomas athomas at thinkspatial.com.au
Tue Dec 12 01:24:18 PST 2017


Following up on Kurt's email:

> It would be worthwhile to (even if only briefly) discuss the use each
> new feature before starting to committing changes.

I would like to begin to use the following C++11 features in new code:

--

1. std::unique_ptr

Up until now, the DXF driver uses containers such as the following:

  std::vector<OGRDXFFeature *> apoMyList;

One has to remember to delete the elements of the container when they
are no longer required. This has led to many memory leak issues that
have been picked up by OSS-Fuzz, and makes programming generally
difficult and annoying. I'd like to start using the following instead:

  std::vector<std::unique_ptr<OGRDXFFeature>> apoMyList;

Hopefully this will help to reduce the incidence of memory leaks.

--

2. auto

I'd like to not have to write

  for( std::vector<std::unique_ptr<OGRFeature>>::iterator oIt =
       apoMyList.begin();

and simply write

  for( auto oIt = apoMyList.begin();

--

At this time I do not propose to migrate any code, but to use these
features only in newly-written and modified code for the time being.

Here is an example commit using these new features:

https://github.com/ThinkSpatial/gdal/commit/905d1690657de820d994eb95076b3fbea5f388bb

Alan

-- 
Alan Thomas
Software Developer
ThinkSpatial
http://www.thinkspatial.com.au


More information about the gdal-dev mailing list