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

Fix final gradient ownership leak #4494

Merged
merged 1 commit into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions libs/escape-from-vstgui/include/efvg/escape_from_vstgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ inline void printStack(const char *where)
char **strs = backtrace_symbols(callstack, frames);
std::ostringstream oss;
oss << "----- " << where << " -----\n";
for (i = 3; i < frames - 1 && i < 20; ++i)
for (i = 0; i < frames - 1 && i < 20; ++i)
{
oss << strs[i] << "\n";
}
Expand All @@ -93,7 +93,7 @@ struct DebugAllocRecord
char **strs = backtrace_symbols(callstack, frames);
std::ostringstream oss;
oss << "----- " << where << " -----\n";
for (i = 3; i < frames - 1 && i < 20; ++i)
for (i = 0; i < frames - 1 && i < 20; ++i)
{
oss << strs[i] << "\n";
}
Expand Down Expand Up @@ -1331,6 +1331,10 @@ struct CControl : public CView
}
~CControl()
{
if (mGradient)
mGradient->forget();
if (mGradientHL)
mGradientHL->forget();
/* if (bg)
bg->forget();
It seems that is not the semantic of CControl!
Expand Down Expand Up @@ -1404,8 +1408,25 @@ struct CControl : public CView
COLPAIR(TextColorHighlighted);
COLPAIR(FrameColorHighlighted);

GSPAIR(Gradient, CGradient *, CGradient *, nullptr);
GSPAIR(GradientHighlighted, CGradient *, CGradient *, nullptr);
CGradient *mGradient = nullptr;
CGradient *getGradient() const { return mGradient; }
virtual void setGradient(CGradient *g)
{
g->remember();
if (mGradient)
mGradient->forget();
mGradient = g;
}

CGradient *mGradientHL = nullptr;
CGradient *getGradientHighlighted() const { return mGradientHL; }
virtual void setGradientHighlighted(CGradient *g)
{
g->remember();
if (mGradientHL)
mGradientHL->forget();
mGradientHL = g;
}

COLPAIR(FillColor);
COLPAIR(TextColor);
Expand Down
48 changes: 43 additions & 5 deletions src/gui/CTextButtonWithHover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ template <typename T> struct HoverGuard
{
HoverGuard() {}

void activate(std::function<T()> getf, std::function<void(T &)> isetf, T &overr)
void activate(std::function<T()> getf, std::function<void(T)> isetf, T overr)
{
active = true;
setf = isetf;
Expand All @@ -30,11 +30,42 @@ template <typename T> struct HoverGuard
~HoverGuard()
{
if (active)
{
setf(orig);
}
}

bool active = false;
std::function<void(T &)> setf;
std::function<void(T)> setf;
T orig;
};

template <typename T> struct HoverGuardRF
{
HoverGuardRF() {}

void activate(std::function<T()> getf, std::function<void(T)> isetf, T overr)
{
active = true;
setf = isetf;
orig = getf();
if (orig)
orig->remember();
setf(overr);
}

~HoverGuardRF()
{
if (active)
{
setf(orig);
if (orig)
orig->forget();
}
}

bool active = false;
std::function<void(T)> setf;
T orig;
};

Expand All @@ -45,15 +76,22 @@ void CTextButtonWithHover::draw(VSTGUI::CDrawContext *context)
HoverGuard<T> hg_##x; \
if (hc_##x.isSet) \
{ \
hg_##x.activate([this]() { return get##x(); }, [this](T &c) { set##x(c); }, hc_##x.item); \
hg_##x.activate([this]() { return get##x(); }, [this](T c) { set##x(c); }, hc_##x.item); \
}

#define DO_HOVERRF(x, T) \
HoverGuardRF<T> hg_##x; \
if (hc_##x.isSet) \
{ \
hg_##x.activate([this]() { return get##x(); }, [this](T c) { set##x(c); }, hc_##x.item); \
}

if (isHovered)
{
DO_HOVER(Gradient, VSTGUI::CGradient *);
DO_HOVERRF(Gradient, VSTGUI::CGradient *);
DO_HOVER(FrameColor, VSTGUI::CColor);
DO_HOVER(TextColor, VSTGUI::CColor);
DO_HOVER(GradientHighlighted, VSTGUI::CGradient *);
DO_HOVERRF(GradientHighlighted, VSTGUI::CGradient *);
DO_HOVER(FrameColorHighlighted, VSTGUI::CColor);
DO_HOVER(TextColorHighlighted, VSTGUI::CColor);
CTextButton::draw(context);
Expand Down
30 changes: 12 additions & 18 deletions src/gui/CTextButtonWithHover.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ class CTextButtonWithHover : public VSTGUI::CTextButton
{
if (isSet && item)
{
// item->forget();
item->forget();
item = nullptr;
}
}
T item = T();
void set(T v)
{
isSet = true;
v->remember();
if (item)
item->forget();
item = v;
}
T item = nullptr;
bool isSet = false;
};

Expand All @@ -48,17 +56,7 @@ class CTextButtonWithHover : public VSTGUI::CTextButton
: CTextButton(r, l, tag, lab)
{
}
~CTextButtonWithHover()
{
if (getGradient())
{
// getGradient()->forget();
}
if (getGradientHighlighted())
{
// getGradientHighlighted()->forget();
}
}
~CTextButtonWithHover() {}
VSTGUI::CMouseEventResult onMouseEntered(VSTGUI::CPoint &where,
const VSTGUI::CButtonState &buttons) override;
void draw(VSTGUI::CDrawContext *context) override;
Expand All @@ -70,11 +68,7 @@ class CTextButtonWithHover : public VSTGUI::CTextButton
*/
#define ADD_HOVER(m, T) \
OptionalForget<T> hc_##m; \
void setHover##m(T c) \
{ \
hc_##m.item = c; \
hc_##m.isSet = true; \
}
void setHover##m(T c) { hc_##m.set(c); }

#define ADD_HOVER_CR(m, T) \
Optional<T> hc_##m; \
Expand Down
7 changes: 6 additions & 1 deletion src/gui/MSEGEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,12 @@ struct MSEGCanvas : public CControl,
dc->setLineStyle(
CLineStyle(CLineStyle::kLineCapButt, CLineStyle::kLineJoinMiter, 0, 2, dashes));
#else
std::cout << "FIXME: Dashed Lines" << std::endl;
static bool warned = false;
if (!warned)
{
std::cout << "FIXME: Dashed Lines " << __FILE__ << __LINE__ << std::endl;
warned = true;
}
#endif
}

Expand Down
14 changes: 14 additions & 0 deletions src/gui/SurgeGUIEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7249,10 +7249,15 @@ void SurgeGUIEditor::addEditorOverlay(VSTGUI::CView *c, std::string editorTitle,
b->setHoverFrameColor(hovbtnborder);
b->setHoverTextColor(hovbtntext);
b->setGradientHighlighted(presscg);
b->setHoverGradientHighlighted(presscg);
b->setFrameColorHighlighted(pressbtnborder);
b->setTextColorHighlighted(pressbtntext);
b->setRoundRadius(CCoord(3.f));

presscg->forget();
hovcg->forget();
cg->forget();

innerc->addView(b);
}

Expand All @@ -7279,6 +7284,7 @@ void SurgeGUIEditor::addEditorOverlay(VSTGUI::CView *c, std::string editorTitle,
editorOverlay.push_back(std::make_pair(editorTag, editorOverlayC));
editorOverlayOnClose[editorOverlayC] = onClose;
editorOverlayContentsWeakReference[editorOverlayC] = c;
editorOverlayContentsWeakReference[editorOverlayC] = c;
}

std::string SurgeGUIEditor::getDisplayForTag(long tag)
Expand Down Expand Up @@ -7451,6 +7457,10 @@ void SurgeGUIEditor::promptForMiniEdit(const std::string &value, const std::stri
kb->setTextColorHighlighted(pressbtntext);
kb->setRoundRadius(CCoord(3.f));
bg->addView(kb);

presscg->forget();
hovcg->forget();
cg->forget();
}

void SurgeGUIEditor::swapControllers(int t1, int t2)
Expand Down Expand Up @@ -7633,6 +7643,10 @@ void SurgeGUIEditor::makeStorePatchDialog()
kb->setTextColorHighlighted(pressbtntext);
kb->setRoundRadius(CCoord(3.f));

cg->forget();
presscg->forget();
hovcg->forget();

saveDialog->addView(pnamelbl);
saveDialog->addView(pcatlbl);
saveDialog->addView(pauthlbl);
Expand Down