Skip to content

Commit

Permalink
prevent segmentation fault in level2osc when unloading
Browse files Browse the repository at this point in the history
  • Loading branch information
gisogrimm committed May 15, 2024
1 parent 95b06e8 commit f227d61
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions plugins/src/tascar_ap_level2osc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ class level2osc_t : public TASCAR::audioplugin_base_t {
lo_message msg;
lo_arg** oscmsgargv;
std::thread thread;
std::atomic_bool run_thread = true;
std::atomic_bool run_thread;
std::atomic_bool is_prepared;
void sendthread();
std::mutex mtx;
std::mutex mtxmsg;
std::condition_variable cond;
std::atomic_bool has_data = false;
std::vector<TASCAR::levelmeter_t> sigcopy;
Expand All @@ -69,6 +71,8 @@ class level2osc_t : public TASCAR::audioplugin_base_t {
level2osc_t::level2osc_t(const TASCAR::audioplugin_cfg_t& cfg)
: audioplugin_base_t(cfg)
{
run_thread = true;
is_prepared = false;
weights.push_back(TASCAR::levelmeter::Z);
GET_ATTRIBUTE_BOOL(sendwhilestopped, "Send also when transport is stopped");
GET_ATTRIBUTE(skip, "", "Skip frames");
Expand Down Expand Up @@ -100,22 +104,25 @@ void level2osc_t::sendthread()
while(run_thread) {
cond.wait_for(lk, 100ms);
if(has_data) {
// pack data:
oscmsgargv[0]->f = currenttime;
for(size_t ch = 0; ch < sigcopy.size(); ++ch) {
switch(imode) {
case dbspl:
oscmsgargv[ch + 1]->f = sigcopy[ch].spldb();
break;
case rms:
oscmsgargv[ch + 1]->f = sigcopy[ch].rms();
break;
case max:
oscmsgargv[ch + 1]->f = sigcopy[ch].maxabs();
break;
std::lock_guard<std::mutex> lock(mtxmsg);
if(is_prepared) {
// pack data:
oscmsgargv[0]->f = currenttime;
for(size_t ch = 0; ch < sigcopy.size(); ++ch) {
switch(imode) {
case dbspl:
oscmsgargv[ch + 1]->f = sigcopy[ch].spldb();
break;
case rms:
oscmsgargv[ch + 1]->f = sigcopy[ch].rms();
break;
case max:
oscmsgargv[ch + 1]->f = sigcopy[ch].maxabs();
break;
}
}
lo_send_message(lo_addr, path.c_str(), msg);
}
lo_send_message(lo_addr, path.c_str(), msg);
has_data = false;
}
}
Expand All @@ -124,6 +131,7 @@ void level2osc_t::sendthread()
void level2osc_t::configure()
{
audioplugin_base_t::configure();
std::lock_guard<std::mutex> lock(mtxmsg);
sigcopy.clear();
msg = lo_message_new();
// time:
Expand All @@ -139,10 +147,13 @@ void level2osc_t::configure()
// sigcopy.back().set_weight(weights[kweight]);
}
oscmsgargv = lo_message_get_argv(msg);
is_prepared = true;
}

void level2osc_t::release()
{
std::lock_guard<std::mutex> lock(mtxmsg);
is_prepared = false;
lo_message_free(msg);
sigcopy.clear();
audioplugin_base_t::release();
Expand Down

0 comments on commit f227d61

Please sign in to comment.