Opened 17 years ago

Closed 15 years ago

#734 closed defect (duplicate)

Memory leak related with reference counting

Reported by: mbrudka@… Owned by: warmerdam
Priority: normal Milestone:
Component: OGR_SRS Version: unspecified
Severity: normal Keywords:

Description (last modified by warmerdam)

OGR uses reference counting for some object to manage the life cycle of objects as well as to 
decrease OGR memory requirements.
In particular SpatialReferenceSystems are reference counted. While reference counting is 
consistent in OGRProj4CT, namely
OGRSpatialReference are deleted when counter falls below 1, in OGRGeometry eg. 
OGRGeometry::~OGRGeometry and
OGRGeometry::assignSpatialReference a counter is released without checking if 
OGRSpatialReference should be deleted. This may
result in memory leak.

   Analyze places, where Reference/Dereference method is called.

The quick fix is to check if reference counter falls below 1 and eventually delete the object eg.

   if( poSRS != NULL )
       if ( poSRS->Dereference() <= 0 )
         delete poSRS;

However, a better way to manage reference counted objects is to use consistently in GDAL 
intrusive smart pointers.
If the dependency on boost libraries is accepted, then I propose to use boost::instrusive_ptr
( If this dependency is for some reason 
undesirable, then a
simple template (boost::instrusive_ptr is 273 lines long) for such pointers can be written. I strongly 
recommend such
technique, as this is simple way to avoid related with reference counted memory management.

Change History (1)

comment:2 by warmerdam, 15 years ago

Description: modified (diff)
Priority: highnormal
Resolution: duplicate
Severity: majornormal
Status: newclosed

This appears to be a duplicate of ticket #738.

Note: See TracTickets for help on using tickets.