MyControlFactory class

A user interface toolkit mainly for audio plug-ins (VST, AudioUnit, etc).
Post Reply
Jay
Posts: 3
Joined: Tue Jun 02, 2020 8:51 am

MyControlFactory class

Post by Jay »

Hello,

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

https://steinbergmedia.github.io/vst3_d ... _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

Arne Scheffler
Posts: 417
Joined: Mon Jun 20, 2016 7:53 am

Re: MyControlFactory class

Post by Arne Scheffler »

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

Jay
Posts: 3
Joined: Tue Jun 02, 2020 8:51 am

Re: MyControlFactory class

Post by Jay »

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

Arne Scheffler
Posts: 417
Joined: Mon Jun 20, 2016 7:53 am

Re: MyControlFactory class

Post by Arne Scheffler »

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:

Code: Select all

class MyHozslide : public CSlider {
public:
  MyHozslide (const CRect& r);
};
And you implement it like this:

Code: Select all

MyHozslide::MyHozslide (const CRect& r)
: CSlider (r, nullptr, -1, 0, 1, nullptr, nullptr)
{}
Then in your view creator you use it like this:

Code: Select all

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

Jay
Posts: 3
Joined: Tue Jun 02, 2020 8:51 am

Re: MyControlFactory class

Post by Jay »

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

Post Reply