-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/fix memory leak #64
Conversation
interfaces/datastructure/Image.h
Outdated
@@ -37,7 +37,7 @@ class SOLARFRAMEWORK_API ImageInternal { | |||
ImageInternal() = default; | |||
explicit ImageInternal(uint32_t size); | |||
explicit ImageInternal(void* data, uint32_t size); | |||
~ImageInternal() = default; | |||
~ImageInternal(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make it virtual
src/datastructure/Image.cpp
Outdated
@@ -419,14 +428,15 @@ FrameworkReturnCode Image::load(std::string imagePath) | |||
m_internalImpl = utils::make_shared<ImageInternal>(); | |||
m_internalImpl->setData(pixels, spec.image_bytes(true)); | |||
in->close(); | |||
delete[] pixels; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you fixed a case, if everything went OK, but at line 402, for exemple, where there is a return statement, resulting in a memory leak (same thing if any exception occurs). You could put everything in a try
block and put the delete[]
in a finally, but we should avoid using new
/delete
as much as possible.
Solutions might be to either
-
use a
std::unique_ptr<unsigned char[]>
(I think that might require a trick consisiting in explicitely give a deallocator, but I'm not sure).
Here a code snippet with an array of ints. Or better:std::make_unique<unsigned char[]>(buffersize)
. -
use a
std::vector
you initialize with thebuffersize
capacity, and give access to openimageio to the internal array withdata
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a good idea to use std::unique_ptr<unsigned char[]> pixels(new unsigned char[size]).
src/datastructure/Image.cpp
Outdated
@@ -467,6 +477,9 @@ void Image::load(Archive & ar, const unsigned int version) | |||
m_internalImpl = utils::make_shared<ImageInternal>(); | |||
m_internalImpl->setData(pixels, spec.image_bytes(true)); | |||
in->close(); | |||
decodingBuffer.clear(); | |||
memreader.close(); | |||
delete[] pixels; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above.
No description provided.