Skip to content

Commit 7429cb8

Browse files
DomClarkzonkmachine
authored andcommitted
Fix segfault on VST plugin I/O change
1 parent fe98a9a commit 7429cb8

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

include/RemotePlugin.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ enum RemoteMessageIDs
424424
IdChangeSharedMemoryKey,
425425
IdChangeInputCount,
426426
IdChangeOutputCount,
427+
IdChangeInputOutputCount,
427428
IdShowUI,
428429
IdHideUI,
429430
IdSaveSettingsToString,
@@ -919,6 +920,15 @@ class RemotePluginClient : public RemotePluginBase
919920
sendMessage( message( IdChangeOutputCount ).addInt( _i ) );
920921
}
921922

923+
void setInputOutputCount( int i, int o )
924+
{
925+
m_inputCount = i;
926+
m_outputCount = o;
927+
sendMessage( message( IdChangeInputOutputCount )
928+
.addInt( i )
929+
.addInt( o ) );
930+
}
931+
922932
virtual int inputCount() const
923933
{
924934
return m_inputCount;

plugins/vst_base/RemoteVstPlugin.cpp

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,26 @@ class RemoteVstPlugin : public RemotePluginClient
249249
pthread_mutex_unlock( &m_pluginLock );
250250
}
251251

252+
inline void lockShm()
253+
{
254+
pthread_mutex_lock( &m_shmLock );
255+
}
256+
257+
inline void unlockShm()
258+
{
259+
pthread_mutex_unlock( &m_shmLock );
260+
}
261+
262+
inline bool isShmValid()
263+
{
264+
return m_shmValid;
265+
}
266+
267+
inline void setShmIsValid( bool valid )
268+
{
269+
m_shmValid = valid;
270+
}
271+
252272
inline bool isProcessing() const
253273
{
254274
return m_processing;
@@ -347,6 +367,9 @@ class RemoteVstPlugin : public RemotePluginClient
347367
float * * m_inputs;
348368
float * * m_outputs;
349369

370+
pthread_mutex_t m_shmLock;
371+
bool m_shmValid;
372+
350373
typedef std::vector<VstMidiEvent> VstMidiEventList;
351374
VstMidiEventList m_midiEvents;
352375

@@ -392,6 +415,8 @@ RemoteVstPlugin::RemoteVstPlugin( const char * socketPath ) :
392415
m_shouldGiveIdle( false ),
393416
m_inputs( NULL ),
394417
m_outputs( NULL ),
418+
m_shmLock(),
419+
m_shmValid( false ),
395420
m_midiEvents(),
396421
m_bpm( 0 ),
397422
m_currentSamplePos( 0 ),
@@ -402,6 +427,7 @@ RemoteVstPlugin::RemoteVstPlugin( const char * socketPath ) :
402427

403428
{
404429
pthread_mutex_init( &m_pluginLock, NULL );
430+
pthread_mutex_init( &m_shmLock, NULL );
405431

406432
__plugin = this;
407433

@@ -500,6 +526,7 @@ RemoteVstPlugin::~RemoteVstPlugin()
500526
delete[] m_inputs;
501527
delete[] m_outputs;
502528

529+
pthread_mutex_destroy( &m_shmLock );
503530
pthread_mutex_destroy( &m_pluginLock );
504531
}
505532

@@ -836,6 +863,16 @@ void RemoteVstPlugin::process( const sampleFrame * _in, sampleFrame * _out )
836863

837864
// now we're ready to fetch sound from VST-plugin
838865

866+
lock();
867+
lockShm();
868+
869+
if( !isShmValid() )
870+
{
871+
unlockShm();
872+
unlock();
873+
return;
874+
}
875+
839876
for( int i = 0; i < inputCount(); ++i )
840877
{
841878
m_inputs[i] = &((float *) _in)[i * bufferSize()];
@@ -847,8 +884,6 @@ void RemoteVstPlugin::process( const sampleFrame * _in, sampleFrame * _out )
847884
memset( m_outputs[i], 0, bufferSize() * sizeof( float ) );
848885
}
849886

850-
lock();
851-
852887
#ifdef OLD_VST_SDK
853888
if( m_plugin->flags & effFlagsCanReplacing )
854889
{
@@ -864,6 +899,7 @@ void RemoteVstPlugin::process( const sampleFrame * _in, sampleFrame * _out )
864899
}
865900
#endif
866901

902+
unlockShm();
867903
unlock();
868904

869905
m_currentSamplePos += bufferSize();
@@ -1380,14 +1416,19 @@ void RemoteVstPlugin::loadChunkFromFile( const std::string & _file, int _len )
13801416

13811417
void RemoteVstPlugin::updateInOutCount()
13821418
{
1419+
lockShm();
1420+
1421+
setShmIsValid( false );
1422+
1423+
unlockShm();
1424+
13831425
delete[] m_inputs;
13841426
delete[] m_outputs;
13851427

13861428
m_inputs = NULL;
13871429
m_outputs = NULL;
13881430

1389-
setInputCount( inputCount() );
1390-
setOutputCount( outputCount() );
1431+
setInputOutputCount( inputCount(), outputCount() );
13911432

13921433
char buf[64];
13931434
sprintf( buf, "inputs: %d output: %d\n", inputCount(), outputCount() );
@@ -1842,6 +1883,11 @@ DWORD WINAPI RemoteVstPlugin::processingThread( LPVOID _param )
18421883
{
18431884
_this->processMessage( m );
18441885
}
1886+
else if( m.id == IdChangeSharedMemoryKey )
1887+
{
1888+
_this->processMessage( m );
1889+
_this->setShmIsValid( true );
1890+
}
18451891
else
18461892
{
18471893
PostMessage( __MessageHwnd,

src/core/RemotePlugin.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,12 @@ bool RemotePlugin::processMessage( const message & _m )
481481
resizeSharedProcessingMemory();
482482
break;
483483

484+
case IdChangeInputOutputCount:
485+
m_inputCount = _m.getInt( 0 );
486+
m_outputCount = _m.getInt( 1 );
487+
resizeSharedProcessingMemory();
488+
break;
489+
484490
case IdDebugMessage:
485491
fprintf( stderr, "RemotePlugin::DebugMessage: %s",
486492
_m.getString( 0 ).c_str() );

0 commit comments

Comments
 (0)