Skip to content

Commit d196c4d

Browse files
author
Your Name
committed
bugfix pass: fix dangling pointers resulting from not realizing the host is allowed to pass multiple host pointers back, and a multi-window imgui thing
1 parent cdef0e7 commit d196c4d

File tree

20 files changed

+84
-63
lines changed

20 files changed

+84
-63
lines changed

clapeze/examples/effectExample.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class MyProcessor : public clapeze::EffectProcessor<MyParamsHandle> {
129129
*/
130130
class MyPlugin : public clapeze::EffectPlugin {
131131
public:
132-
explicit MyPlugin(clapeze::PluginHost& host) : EffectPlugin(host) {}
132+
explicit MyPlugin(const clap_plugin_descriptor_t& meta) : EffectPlugin(meta) {}
133133
~MyPlugin() = default;
134134

135135
protected:

clapeze/examples/instrumentExample.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class Processor : public clapeze::InstrumentProcessor<ParamsHandle> {
105105

106106
class Plugin : public clapeze::InstrumentPlugin {
107107
public:
108-
explicit Plugin(clapeze::PluginHost& host) : clapeze::InstrumentPlugin(host) {}
108+
explicit Plugin(const clap_plugin_descriptor_t& meta) : clapeze::InstrumentPlugin(meta) {}
109109
~Plugin() = default;
110110

111111
protected:

clapeze/include/clapeze/basePlugin.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class BasePlugin;
1616

1717
struct PluginEntry {
1818
clap_plugin_descriptor_t meta;
19-
BasePlugin* (*factory)(PluginHost& host);
19+
BasePlugin* (*factory)(const clap_plugin_descriptor_t& meta);
2020
};
2121

2222
/**
@@ -27,7 +27,7 @@ struct PluginEntry {
2727
*/
2828
class BasePlugin {
2929
public:
30-
explicit BasePlugin(PluginHost& host) : mHost(host) {}
30+
explicit BasePlugin(const clap_plugin_descriptor_t& desc);
3131
virtual ~BasePlugin() = default;
3232

3333
protected:
@@ -73,7 +73,7 @@ class BasePlugin {
7373

7474
/* helpers */
7575
public:
76-
const clap_plugin_t* GetOrCreatePluginObject(const clap_plugin_descriptor_t* meta);
76+
const clap_plugin_t* GetPluginObject();
7777

7878
template <typename TPlugin = BasePlugin>
7979
static TPlugin& GetFromPluginObject(const clap_plugin_t* plugin);
@@ -85,15 +85,16 @@ class BasePlugin {
8585
const void* TryGetExtension(const char* name);
8686

8787
BaseProcessor& GetProcessor() const;
88+
void SetHost(const clap_host_t* host);
8889
PluginHost& GetHost();
8990
const PluginHost& GetHost() const;
9091

9192
/* internal implementation*/
9293
private:
93-
std::unique_ptr<clap_plugin_t> mPlugin{};
94+
clap_plugin_t mPlugin;
9495
std::unordered_map<std::string, std::unique_ptr<BaseFeature>> mFeatures{};
9596
std::unordered_map<std::string, const void*> mExtensions{};
96-
PluginHost& mHost;
97+
std::unique_ptr<PluginHost> mHost{};
9798
std::unique_ptr<BaseProcessor> mProcessor{};
9899

99100
static bool _init(const clap_plugin* plugin);

clapeze/include/clapeze/effectPlugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class EffectProcessor : public BaseProcessor {
5353
/* pre-configured for simple stereo effects */
5454
class EffectPlugin : public BasePlugin {
5555
public:
56-
explicit EffectPlugin(PluginHost& host) : BasePlugin(host) {}
56+
explicit EffectPlugin(const clap_plugin_descriptor_t& meta) : BasePlugin(meta) {}
5757
~EffectPlugin() = default;
5858

5959
protected:

clapeze/include/clapeze/entryPoint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const void* _get_factory(const char* factoryId);
4141
#define CLAPEZE_REGISTER_PLUGIN(TPlugin, descriptor) \
4242
static bool CLAPEZE_DETAIL_CAT(_plugin_register_, __LINE__) = \
4343
(clapeze::registerPlugin(clapeze::PluginEntry{ \
44-
descriptor, ([](clapeze::PluginHost& host) -> clapeze::BasePlugin* { return new TPlugin(host); })}), \
44+
descriptor, ([](const clap_plugin_descriptor_t& meta) -> clapeze::BasePlugin* { return new TPlugin(meta); })}), \
4545
true)
4646

4747
/**

clapeze/include/clapeze/instrumentPlugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class InstrumentProcessor : public BaseProcessor {
8989
/* pre-configured for simple stereo instruments */
9090
class InstrumentPlugin : public BasePlugin {
9191
public:
92-
explicit InstrumentPlugin(PluginHost& host) : BasePlugin(host) {}
92+
explicit InstrumentPlugin(const clap_plugin_descriptor_t& meta) : BasePlugin(meta) {}
9393
~InstrumentPlugin() = default;
9494

9595
protected:

clapeze/src/basePlugin.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010

1111
namespace clapeze {
1212

13+
BasePlugin::BasePlugin(const clap_plugin_descriptor_t& meta)
14+
: mPlugin{&meta,
15+
this,
16+
&_init,
17+
&_destroy,
18+
&_activate,
19+
&_deactivate,
20+
&_start_processing,
21+
&_stop_processing,
22+
&_reset,
23+
&_process,
24+
&_get_extension,
25+
&_on_main_thread} {}
26+
1327
BaseFeature* BasePlugin::TryGetFeature(const char* name) {
1428
if (auto search = mFeatures.find(name); search != mFeatures.end()) {
1529
return search->second.get();
@@ -40,11 +54,14 @@ const void* BasePlugin::TryGetExtension(const char* name) {
4054
}
4155
}
4256

57+
void BasePlugin::SetHost(const clap_host_t* host) {
58+
mHost = std::make_unique<PluginHost>(host);
59+
}
4360
PluginHost& BasePlugin::GetHost() {
44-
return mHost;
61+
return *mHost;
4562
}
4663
const PluginHost& BasePlugin::GetHost() const {
47-
return mHost;
64+
return *mHost;
4865
}
4966

5067
bool BasePlugin::Init() {
@@ -54,7 +71,7 @@ bool BasePlugin::Init() {
5471

5572
bool BasePlugin::ValidateConfig() {
5673
if (mProcessor == nullptr) {
57-
mHost.Log(LogSeverity::Fatal,
74+
mHost->Log(LogSeverity::Fatal,
5875
"mProcessor is null. did you forget to call ConfigProcessor() in your Config method?");
5976
return false;
6077
}
@@ -186,23 +203,8 @@ void BasePlugin::_on_main_thread(const clap_plugin* plugin) {
186203
self.OnMainThread();
187204
}
188205

189-
const clap_plugin_t* BasePlugin::GetOrCreatePluginObject(const clap_plugin_descriptor_t* meta) {
190-
if (mPlugin.get() == nullptr) {
191-
const clap_plugin_t pluginObject = {meta,
192-
this,
193-
&_init,
194-
&_destroy,
195-
&_activate,
196-
&_deactivate,
197-
&_start_processing,
198-
&_stop_processing,
199-
&_reset,
200-
&_process,
201-
&_get_extension,
202-
&_on_main_thread};
203-
mPlugin = std::make_unique<clap_plugin_t>(pluginObject);
204-
}
205-
return mPlugin.get();
206+
const clap_plugin_t* BasePlugin::GetPluginObject() {
207+
return &mPlugin;
206208
}
207209

208210
} // namespace clapeze

clapeze/src/entryPoint.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ std::string& sPluginPath() {
2020
namespace PluginFactory {
2121

2222
uint32_t get_plugin_count(const clap_plugin_factory* factory) {
23-
return sPlugins().size();
23+
(void)factory;
24+
return static_cast<uint32_t>(sPlugins().size());
2425
}
2526

2627
const clap_plugin_descriptor_t* get_plugin_descriptor(const clap_plugin_factory* factory, uint32_t index) {
28+
(void)factory;
2729
if (index < sPlugins().size()) {
2830
const PluginEntry& entry = sPlugins()[index];
2931
return &entry.meta;
@@ -32,6 +34,7 @@ const clap_plugin_descriptor_t* get_plugin_descriptor(const clap_plugin_factory*
3234
}
3335

3436
const clap_plugin_t* create_plugin(const clap_plugin_factory* factory, const clap_host_t* host, const char* pluginId) {
37+
(void)factory;
3538
if (!clap_version_is_compatible(host->clap_version)) {
3639
return nullptr;
3740
}
@@ -40,7 +43,10 @@ const clap_plugin_t* create_plugin(const clap_plugin_factory* factory, const cla
4043

4144
for (const auto& entry : sPlugins()) {
4245
if (std::string_view(entry.meta.id) == pluginId) {
43-
return entry.factory(cppHost)->GetOrCreatePluginObject(&entry.meta);
46+
BasePlugin* plugin = entry.factory(entry.meta);
47+
// intentionally using a setter here to hide the host from the factory function
48+
plugin->SetHost(host);
49+
return plugin->GetPluginObject();
4450
}
4551
}
4652

@@ -56,11 +62,13 @@ const clap_plugin_factory_t value = {
5662

5763
namespace PresetFactory {
5864
uint32_t _count(const clap_preset_discovery_factory* factory) {
65+
(void)factory;
5966
return 1;
6067
}
6168

6269
const clap_preset_discovery_provider_descriptor_t* _get_descriptor(const clap_preset_discovery_factory* factory,
6370
uint32_t index) {
71+
(void)factory;
6472
if (index == 0) {
6573
return PresetProvider::Descriptor();
6674
}
@@ -70,6 +78,8 @@ const clap_preset_discovery_provider_descriptor_t* _get_descriptor(const clap_pr
7078
const clap_preset_discovery_provider_t* _create(const clap_preset_discovery_factory* factory,
7179
const clap_preset_discovery_indexer_t* indexer,
7280
const char* provider_id) {
81+
(void)factory;
82+
(void)provider_id;
7383
return PresetProvider::Create(indexer);
7484
}
7585

clapeze/test/factory.test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ TEST(clap_factory, canBeQueriedWhileEmpty) {
3636
namespace {
3737
class MockPlugin : public clapeze::BasePlugin {
3838
public:
39-
explicit MockPlugin(clapeze::PluginHost& host) : BasePlugin(host) {}
39+
explicit MockPlugin(const clap_plugin_descriptor_t& meta) : BasePlugin(meta) {}
4040
void Config() override {};
4141
};
4242
} // namespace
@@ -49,8 +49,8 @@ TEST(clap_factory, canCreatePlugins) {
4949
.id = "my_id",
5050
.name = "My Name",
5151
},
52-
([](clapeze::PluginHost& host) -> clapeze::BasePlugin* {
53-
sPlugin = new MockPlugin(host);
52+
([](const clap_plugin_descriptor_t& meta) -> clapeze::BasePlugin* {
53+
sPlugin = new MockPlugin(meta);
5454
return sPlugin;
5555
})});
5656
ASSERT_TRUE(clap_entry.init(""));
@@ -62,16 +62,16 @@ TEST(clap_factory, canCreatePlugins) {
6262
EXPECT_EQ(desc->clap_version.major, CLAP_VERSION_MAJOR);
6363
EXPECT_EQ(desc->clap_version.minor, CLAP_VERSION_MINOR);
6464
EXPECT_EQ(desc->clap_version.revision, CLAP_VERSION_REVISION);
65-
EXPECT_EQ(desc->id, "my_id");
66-
EXPECT_EQ(desc->name, "My Name");
65+
EXPECT_EQ(std::string_view(desc->id), "my_id");
66+
EXPECT_EQ(std::string_view(desc->name), "My Name");
6767

6868
clap_host_t mockHost{
6969
.clap_version = CLAP_VERSION_INIT,
7070
.get_extension = [](const struct clap_host* host, const char* extension_id) -> const void* { return nullptr; },
7171
};
7272
EXPECT_EQ(nullptr, factory->create_plugin(factory, &mockHost, "fake_id"));
7373
auto* pluginObject = factory->create_plugin(factory, &mockHost, "my_id");
74-
EXPECT_EQ(sPlugin->GetOrCreatePluginObject(desc), pluginObject);
74+
EXPECT_EQ(sPlugin->GetPluginObject(), pluginObject);
7575

7676
clap_entry.deinit();
7777
}

daw/src/chorus/chorus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class GuiApp : public kitgui::BaseApp {
115115
class Plugin : public EffectPlugin {
116116
public:
117117
static const PluginEntry Entry;
118-
explicit Plugin(PluginHost& host) : EffectPlugin(host) {}
118+
explicit Plugin(const clap_plugin_descriptor_t& meta) : EffectPlugin(meta) {}
119119
~Plugin() = default;
120120

121121
protected:

0 commit comments

Comments
 (0)