1

I am creating a shapefile in C++ 11 using GDAL.

Can I can safely make use of shared_pt< SomeGDALType > instead of SomeGDALType::CreateSomeGDALType() and SomeGDALType::DestroySomeGDALType(); ?

I noticed that the example code follows this pattern for all pointers the library needs:

OGRFeature *poFeature
poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() )

... other code that calls library function with above pointer ...

OGRFeature::DestroyFeature( poFeature );
//end of function.

Since I want my pointers to always be cleaned up nicely, I want to use a shared pointer like so:

std::shared_ptr<OGRFeature>poFeature(OGRFeature::CreateFeature( poLayer->GetLayerDefn()),OGRFeature::DestroyFeature);;

... other code that calls library function with poFeature.get() ...

//end of function

Full code here on Github

This obviates the problem of memory leaks if my code throws an exception. My code compiles, runs and creates a working shapefile.

My question is twofold:

Firstly; Is there a specific reason I want the memory to be allocated and de-allocated specifically by the library, (thereby allocating it possibly on another heap) which is what happens when I call the example's create and destroy functions?

Secondly; Can problems, beyond obvious programming mistakes on my end, arise as a result of my proposed alternative implementation?

Joeppie
  • 438
  • 3
  • 16
  • 1
    A lot of times when you see libraries have `Create()` and `Destroy()` methods it is because they want to create and destroy the object in the same dll. If you allocate the object in one dll, then destroy it in another, you'd have to guarantee binary comparability of the object, which is [really](https://stackoverflow.com/questions/37149479/when-do-we-break-binary-compatibility) [hard](https://stackoverflow.com/questions/1774911/how-to-design-a-c-api-for-binary-compatible-extensibility). – Cory Kramer Aug 29 '17 at 14:29
  • @CoryKramer I know what the same allocator must be used to create and delete resources, but that is the case whether I both create and delete the pointer, or the library does the same. Btw; I just edited my code to call the create an delete functions (I did not previously realize this was an option with the std::shared_ptr) – Joeppie Aug 29 '17 at 14:34
  • 1
    One problem i encountered in a similar situation was that the library (not gdal) was freeing some stuff automatically at 'parent' object destruction, resulting in double-frees. Does GDAL state that you are in charge of destroying all of those instances? – bartoli Aug 29 '17 at 15:39
  • This is a good question @bartoli , I need to investigate. It does seem that I need to dispose of the object I did mention, however, using Valgrind, I determined that my program does not create a memoryleak; however, It does create other objects: as seen in the C++ example burried in this page somewhat near the bottom: http://gdal.org/1.11/ogr/ogr_apitut.html : poDS->CreateLayer() is called, which returns a pointer and is never deleted, from what I can tell, not even by the destructor of the poDS' class. This confounds me, and I must be missing something. – Joeppie Aug 29 '17 at 17:58

0 Answers0