Skip to content

Commit 5fe60f9

Browse files
authored
Fix memory leaks in exrstdattr and example code (#1649)
Caught by the address sanitizer, worth addressing in the example code as good programming practice. Signed-off-by: Cary Phillips <[email protected]>
1 parent 32de807 commit 5fe60f9

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/bin/exrstdattr/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,9 @@ main (int argc, char** argv)
848848
outPart.copyPixels (inPart);
849849
}
850850
}
851+
852+
for (int i=0; i<attrs.size(); i++)
853+
delete attrs[i].attr;
851854
}
852855
catch (const exception& e)
853856
{

src/examples/multipartExamples.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,18 @@ void resizeDeepBuffers (
314314
}
315315
}
316316

317+
template <typename T>
318+
void freeDeepBuffers (list<Array2D<T *>> & channels)
319+
{
320+
for (auto i = channels.begin (); i != channels.end (); i++)
321+
{
322+
Array2D<T*> & channel = *i;
323+
for (int y = 0; y < channel.height (); y++)
324+
for (int x = 0; x < channel.width (); x++)
325+
delete[] channel[y][x];
326+
}
327+
}
328+
317329
template <typename T>
318330
void modifyChannels(list<Array2D<T>> & channels, T delta)
319331
{
@@ -474,6 +486,10 @@ void modifyMultipart ()
474486
outputPart.setFrameBuffer (frameBuffer);
475487
outputPart.writeTiles (0, outputPart.numXTiles () - 1, 0, outputPart.numYTiles () - 1);
476488
}
489+
490+
freeDeepBuffers(intChannels);
491+
freeDeepBuffers(halfChannels);
492+
freeDeepBuffers(floatChannels);
477493
}
478494
}
479495
}

0 commit comments

Comments
 (0)