MyControlFactory class

Hello,

I have been creating some custom views following the helpful documentation supplied on this page;

https://steinbergmedia.github.io/vst3_doc/vstgui/html/md_page_create_your_own_ui_view.html

I was successfully able to create some knobs and sliders which was terrific.

However I’ve hit a serious issue, involving a memory leak which is stemming from my implementation of MyControlFactory.cpp

My slider control registration class looks like this.

class MyControlFactory4 : public ViewCreatorAdapter {
public: // register this class with the view factory
MyControlFactory4() { UIViewFactory::registerViewCreator(*this); }

IdStringPtr getViewName() const override { return “hoz slide”; }
IdStringPtr getBaseViewName() const override { return UIViewCreator::kCSlider; }

CView* create(const UIAttributes& attributes, const IUIDescription* description) const override {

CSlider *sli = new CSlider(CRect(0, 0, 70, 70), nullptr, -1, 0, 1, nullptr, nullptr);

return new MyHozslide(* sli );
}
};

I can see that the problem lies in declaring the slider with new but then not deleting it, which I can’t figure out how to do.

But perhaps there is another way of declaring a CSlider without using new.

Any thought on how to approach this problem would be massively appreciated.

Thanks,
James

Hi,
can you post your declaration of your MyHozslide constructor?
It looks like you do something which is not a good practice when dealing with sub classing. But to be sure we need to see your constructor…

Cheers,
Arne

Hi Arne,

Many thanks for your quick response. Apologies though I should have posted this initially.

I have this in MyControl.h

class MyHozslide : public CSlider {

public: MyHozslide(const CSlider &slider);

void draw(CDrawContext *pContext) override;

CLASS_METHODS(MyHozslide, CSlider, CSliderBase)
};

edit//
for completeness I’m implementing this in MyControl.cpp;

MyHozslide::MyHozslide(const CSlider& slide) : CSlider(slide) {}

void MyHozslide::draw(CDrawContext* pContext) { // — setup the background rectangle

//various code
pContext->drawGraphicsPath(cap, CDrawContext::PathDrawMode::kPathFilled);

setDirty(false);
}
//

Hopefully I’ve just gone off the rails a bit here with the constructor.

Cheers

Hi Jay,
you should follow the same way on how to sub-class as other controls in VSTGUI. In your case you should declare your constructor like this:

class MyHozslide : public CSlider {
public:
  MyHozslide (const CRect& r);
};

And you implement it like this:

MyHozslide::MyHozslide (const CRect& r)
: CSlider (r, nullptr, -1, 0, 1, nullptr, nullptr)
{}

Then in your view creator you use it like this:

CView* create(const UIAttributes& attributes, const IUIDescription* description) const override {
  return new MyHozslide (CRect (0, 0, 70, 70));
}

I hope this helps,
Arne

Hi Arne,
Thankyou, that is perfect. I was able to get this and several other custom Views up and running without errors and 0 memory leaking.
I was missing the key realization that you can just use the CSlider like that in the .cpp implentation.

Many thanks for your good help I had been struggling with this for a while now.

Cheers,
James