Image copy constructor leak

Hello, I’m not sure about what is going on, but the memory is wasted when I do this :

void handleAsyncUpdate() override { //called very often
	// _rendered.reset(new Image()); //no leak
	//this->createComponentSnapshot(getLocalBounds()); // no leak

	//_rendered2.clear();
	//_rendered2.emplace_back(this->createComponentSnapshot(getLocalBounds()));  //no leak

	//_rendered.reset(new Image());
	//*_rendered = createComponentSnapshot(getLocalBounds()); //leak : the memory slowly goes up
       
	_rendered.reset(new Image(this->createComponentSnapshot(getLocalBounds()))); //leak : the memory slowly goes up
}

I have no warnings when I close the application. So is it a silent leak, or something I’m not aware of ?

Is there some reason you are "new"ing Images?

Well, I think I could avoid the new here. But I use a std::unique_ptr so it shouldn’t be a problem

The problem is that Image is a class you are supposed to use as a value and not via pointers. (Unless there’s some good reason to go through pointers.)

Ok, I tested by value and there is no leaks anymore.

_rendered2 = Image(this->createComponentSnapshot(getLocalBounds()));

But even if using new is a bad design, it shouldn’t leak, right ?

In principle it shouldn’t leak, especially if you are using smart pointers, but maybe there’s some subtle issue happening with it.

I’m not sure what’s happening here, but maybe there is a leak with the copy constructor; Avoided in the second case because there is some move operator involved ?

Image is reference counted. It self-deletes. You shouldn’t allocate it on the heap because it does that automatically for its internal pixel data. Look at the implementation of the Image class

in your handleAsyncUpdate() you should just do
_rendered = createComponentSnapshot(getLocalBounds());
where _rendered is Image _rendered;

Also, you shouldn’t use underscores in your variable names, as that character’s use as the start of a variable name is considered reserved for the standard library.

see https://juce.com/discover/stories/coding-standards

Ok ! It makes sense now. So I probably would run into the same kind of issue if I use the ValueTree class with new.

Good to know. My company use the underscore for private class members everywhere so it’s a bit late too change. But I’ll remember it for others projects.

Thank you for your answer !