mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 16:13:46 +01:00
Fix some memleaks from GUIButtonImage (#13564)
* `m_foreground_image` was grabbed, but not dropped in the destructor. * `m_image` was created with new. It is grabbed by itself and by the env (not only by the env!, so it's an owning ptr). This owning ptr also was never dropped.
This commit is contained in:
parent
1b51ff333a
commit
553dc02deb
@ -35,24 +35,18 @@ GUIButtonImage::GUIButtonImage(gui::IGUIEnvironment *environment,
|
|||||||
: GUIButton(environment, parent, id, rectangle, tsrc, noclip)
|
: GUIButton(environment, parent, id, rectangle, tsrc, noclip)
|
||||||
{
|
{
|
||||||
GUIButton::setScaleImage(true);
|
GUIButton::setScaleImage(true);
|
||||||
m_image = new GUIAnimatedImage(environment, this, id, rectangle);
|
m_image = make_irr<GUIAnimatedImage>(environment, this, id, rectangle);
|
||||||
sendToBack(m_image);
|
sendToBack(m_image.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
void GUIButtonImage::setForegroundImage(video::ITexture *image,
|
void GUIButtonImage::setForegroundImage(irr_ptr<video::ITexture> image,
|
||||||
const core::rect<s32> &middle)
|
const core::rect<s32> &middle)
|
||||||
{
|
{
|
||||||
if (image == m_foreground_image)
|
if (image == m_foreground_image)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (image != nullptr)
|
m_foreground_image = std::move(image);
|
||||||
image->grab();
|
m_image->setTexture(m_foreground_image.get());
|
||||||
|
|
||||||
if (m_foreground_image != nullptr)
|
|
||||||
m_foreground_image->drop();
|
|
||||||
|
|
||||||
m_foreground_image = image;
|
|
||||||
m_image->setTexture(image);
|
|
||||||
m_image->setMiddleRect(middle);
|
m_image->setMiddleRect(middle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,8 +61,8 @@ void GUIButtonImage::setFromStyle(const StyleSpec &style)
|
|||||||
video::ITexture *texture = style.getTexture(StyleSpec::FGIMG,
|
video::ITexture *texture = style.getTexture(StyleSpec::FGIMG,
|
||||||
getTextureSource());
|
getTextureSource());
|
||||||
|
|
||||||
setForegroundImage(guiScalingImageButton(driver, texture,
|
setForegroundImage(::grab(guiScalingImageButton(driver, texture,
|
||||||
AbsoluteRect.getWidth(), AbsoluteRect.getHeight()),
|
AbsoluteRect.getWidth(), AbsoluteRect.getHeight())),
|
||||||
style.getRect(StyleSpec::FGIMG_MIDDLE, m_image->getMiddleRect()));
|
style.getRect(StyleSpec::FGIMG_MIDDLE, m_image->getMiddleRect()));
|
||||||
} else {
|
} else {
|
||||||
setForegroundImage();
|
setForegroundImage();
|
||||||
@ -80,7 +74,7 @@ GUIButtonImage *GUIButtonImage::addButton(IGUIEnvironment *environment,
|
|||||||
IGUIElement *parent, s32 id, const wchar_t *text,
|
IGUIElement *parent, s32 id, const wchar_t *text,
|
||||||
const wchar_t *tooltiptext)
|
const wchar_t *tooltiptext)
|
||||||
{
|
{
|
||||||
GUIButtonImage *button = new GUIButtonImage(environment,
|
auto button = make_irr<GUIButtonImage>(environment,
|
||||||
parent ? parent : environment->getRootGUIElement(), id, rectangle, tsrc);
|
parent ? parent : environment->getRootGUIElement(), id, rectangle, tsrc);
|
||||||
|
|
||||||
if (text)
|
if (text)
|
||||||
@ -89,6 +83,5 @@ GUIButtonImage *GUIButtonImage::addButton(IGUIEnvironment *environment,
|
|||||||
if (tooltiptext)
|
if (tooltiptext)
|
||||||
button->setToolTipText(tooltiptext);
|
button->setToolTipText(tooltiptext);
|
||||||
|
|
||||||
button->drop();
|
return button.get();
|
||||||
return button;
|
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include "guiButton.h"
|
#include "guiButton.h"
|
||||||
#include "IGUIButton.h"
|
#include "IGUIButton.h"
|
||||||
#include "guiAnimatedImage.h"
|
#include "guiAnimatedImage.h"
|
||||||
|
#include "irr_ptr.h"
|
||||||
|
|
||||||
using namespace irr;
|
using namespace irr;
|
||||||
|
|
||||||
@ -33,7 +34,7 @@ public:
|
|||||||
s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,
|
s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,
|
||||||
bool noclip = false);
|
bool noclip = false);
|
||||||
|
|
||||||
void setForegroundImage(video::ITexture *image = nullptr,
|
void setForegroundImage(irr_ptr<video::ITexture> image = nullptr,
|
||||||
const core::rect<s32> &middle = core::rect<s32>());
|
const core::rect<s32> &middle = core::rect<s32>());
|
||||||
|
|
||||||
//! Set element properties from a StyleSpec
|
//! Set element properties from a StyleSpec
|
||||||
@ -46,6 +47,6 @@ public:
|
|||||||
const wchar_t *tooltiptext = L"");
|
const wchar_t *tooltiptext = L"");
|
||||||
|
|
||||||
private:
|
private:
|
||||||
video::ITexture *m_foreground_image = nullptr;
|
irr_ptr<video::ITexture> m_foreground_image;
|
||||||
GUIAnimatedImage *m_image;
|
irr_ptr<GUIAnimatedImage> m_image;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user