free-audio / clap

Audio Plugin API
https://cleveraudio.org/
MIT License
1.77k stars 100 forks source link

Is it valid to query extensions after creating but before initializing the plugin? #186

Closed falkTX closed 1 year ago

falkTX commented 1 year ago

I am seeing a few plugins crash under these conditions:

  1. load DSO and call entry points to get a clap_plugin_t pointer
  2. call get_extension to figure out details of the plugin, seeing if it is compatible
  3. call init as needed, otherwise destroy as the plugin is not wanted

For some plugins, the get_extension call before a init is triggering a segmentation fault crash.

Documentation for the plugin factory creation method states:

// Create a clap_plugin by its plugin_id. // The returned pointer must be freed by calling plugin->destroy(plugin); // The plugin is not allowed to use the host callbacks in the create method. // Returns null in case of error.

So we must call the destroy method, but is it valid to destroy the plugin (or do any other calls) before calling init? The init docs say:

// Must be called after creating the plugin.

But IMO such comment should be on the plugin factory side as well. From reading the plugin factory comments, we are lead to think we can create a plugin and simply destroy it afterwards.

robbert-vdh commented 1 year ago

You are indeed not allowed to call clap_plugin::get_extension() before clap_plugin::init() has been called, just like the plugin isn't allowed to call clap_host::get_extension() before the host calls clap_plugin::init().

So we must call the destroy method, but is it valid to destroy the plugin (or do any other calls) before calling init?

You can technically call clap_plugin::destroy() immediately after creating it without calling init() first, but there's be no reason to do that.

falkTX commented 1 year ago

good points. I think mentioning this directly in the header docs is worthwhile.

clap has very strict rules when it comes to threading, the same should be true for the calling order too. will do a PR later if no one else does by then.