[Gdal-dev] Memory leak fixes in ogrfeaturesytle.cpp
Steve Brooks
sbrooks2 at kc.rr.com
Sun Sep 28 00:38:06 EDT 2003
Frank,
I ran across several memory leaks in ogrfeaturestyle.cpp
with a large input file and querying the feature style string I was
getting several thousand memory leaks.
The problem was in two places, first in CreateStyleToolFromStyleString
it was not calling CSLDestroy(papszToken);
Then in the destructors of OGRStyleTool subclasses they were
only calling CPLFree(m_pasStyleValue);
The problem with this is m_pasStyleValue is a vector like class of
several OGRStyleValue's. Inside OGRStyleValue can be a pointer to a
heap allocated string in pszValue, which was the leak.
Below are the changes that fixed these leaks.
Thanks
Steve Brooks
$ diff clean/gdal/ogr/ogrfeaturestyle.cpp
modified/gdal/ogr/ogrfeaturestyle.cpp
432a433
> OGRStyleTool *poStyleTool = NULL;
434,447c435,447
< if (CSLCount(papszToken) <2)
< return NULL;
<
< if (EQUAL(papszToken[0],"PEN"))
< return new OGRStylePen();
< else if (EQUAL(papszToken[0],"BRUSH"))
< return new OGRStyleBrush();
< else if (EQUAL(papszToken[0],"SYMBOL"))
< return new OGRStyleSymbol();
< else if (EQUAL(papszToken[0],"LABEL"))
< return new OGRStyleLabel();
< else
< return NULL;
<
---
> if (CSLCount(papszToken) >= 2)
> {
> if (EQUAL(papszToken[0],"PEN"))
> poStyleTool = new OGRStylePen();
> else if (EQUAL(papszToken[0],"BRUSH"))
> poStyleTool = new OGRStyleBrush();
> else if (EQUAL(papszToken[0],"SYMBOL"))
> poStyleTool = new OGRStyleSymbol();
> else if (EQUAL(papszToken[0],"LABEL"))
> poStyleTool = new OGRStyleLabel();
> }
> CSLDestroy(papszToken);
> return poStyleTool;
1394a1395,1402
> for (int i = 0; i < OGRSTPenLast; i++)
> {
> if (m_pasStyleValue[i].pszValue != NULL)
> {
> CPLFree(m_pasStyleValue[i].pszValue);
> m_pasStyleValue[i].pszValue = NULL;
> }
> }
1414a1423,1430
> for (int i = 0; i < OGRSTBrushLast; i++)
> {
> if (m_pasStyleValue[i].pszValue != NULL)
> {
> CPLFree(m_pasStyleValue[i].pszValue);
> m_pasStyleValue[i].pszValue = NULL;
> }
> }
1434a1451,1458
> for (int i = 0; i < OGRSTSymbolLast; i++)
> {
> if (m_pasStyleValue[i].pszValue != NULL)
> {
> CPLFree(m_pasStyleValue[i].pszValue);
> m_pasStyleValue[i].pszValue = NULL;
> }
> }
1455,1456c1479,1490
< CPLFree(m_pasStyleValue);
<
---
> if (m_pasStyleValue != NULL)
> {
> for (int i = 0; i < OGRSTLabelLast; i++)
> {
> if (m_pasStyleValue[i].pszValue != NULL)
> {
> CPLFree(m_pasStyleValue[i].pszValue);
> m_pasStyleValue[i].pszValue = NULL;
> }
> }
> }
> CPLFree(m_pasStyleValue);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20030927/5f514d5f/attachment.html
More information about the Gdal-dev
mailing list