Skip to content
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

Merged
merged 4 commits into from
Jun 28, 2023
Merged

Feat/fix memory leak #64

merged 4 commits into from
Jun 28, 2023

Conversation

yzhou-bcom
Copy link
Contributor

No description provided.

@@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it virtual

@@ -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;
Copy link
Contributor

@jim-bcom jim-bcom Jun 27, 2023

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 the buffersize capacity, and give access to openimageio to the internal array with data.

Copy link
Contributor Author

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]).

@@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

@yzhou-bcom yzhou-bcom merged commit d69e6a6 into develop Jun 28, 2023
@jim-bcom jim-bcom deleted the feat/fix-memory-leak branch January 24, 2024 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants