Skip to content

Commit

Permalink
Fix memory leaks in exrstdattr and example code (#1649)
Browse files Browse the repository at this point in the history
Caught by the address sanitizer, worth addressing in the example code
as good programming practice.

Signed-off-by: Cary Phillips <[email protected]>
  • Loading branch information
cary-ilm authored Feb 28, 2024
1 parent 32de807 commit 5fe60f9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/bin/exrstdattr/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,9 @@ main (int argc, char** argv)
outPart.copyPixels (inPart);
}
}

for (int i=0; i<attrs.size(); i++)
delete attrs[i].attr;
}
catch (const exception& e)
{
Expand Down
16 changes: 16 additions & 0 deletions src/examples/multipartExamples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@ void resizeDeepBuffers (
}
}

template <typename T>
void freeDeepBuffers (list<Array2D<T *>> & channels)
{
for (auto i = channels.begin (); i != channels.end (); i++)
{
Array2D<T*> & channel = *i;
for (int y = 0; y < channel.height (); y++)
for (int x = 0; x < channel.width (); x++)
delete[] channel[y][x];
}
}

template <typename T>
void modifyChannels(list<Array2D<T>> & channels, T delta)
{
Expand Down Expand Up @@ -474,6 +486,10 @@ void modifyMultipart ()
outputPart.setFrameBuffer (frameBuffer);
outputPart.writeTiles (0, outputPart.numXTiles () - 1, 0, outputPart.numYTiles () - 1);
}

freeDeepBuffers(intChannels);
freeDeepBuffers(halfChannels);
freeDeepBuffers(floatChannels);
}
}
}
Expand Down

0 comments on commit 5fe60f9

Please sign in to comment.