Skip to content

Commit

Permalink
Fix potential crash in ScanParallel()
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusTomlinson committed Nov 19, 2024
1 parent 568f0a8 commit d92d63b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
40 changes: 16 additions & 24 deletions include/dspatch/Circuit.h
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,6 @@ inline void Circuit::SetThreadCount( int threadCount )
{
PauseAutoTick();

if ( _threadCount == 0 && threadCount != 0 )
{
_circuitDirty = true;
}

_threadCount = threadCount;

// stop all threads
Expand Down Expand Up @@ -799,27 +794,24 @@ inline void Circuit::_Optimize()
}

// scan for optimal parallel order -> update _componentsParallel
if ( _threadCount != 0 )
{
std::vector<std::vector<DSPatch::Component*>> componentsMap;
componentsMap.reserve( _components.size() );
std::vector<std::vector<DSPatch::Component*>> componentsMap;
componentsMap.reserve( _components.size() );

int scanPosition;
for ( auto component : _components )
{
component->ScanParallel( componentsMap, scanPosition );
}
for ( auto component : _components )
{
component->EndScan();
}
int scanPosition;
for ( auto component : _components )
{
component->ScanParallel( componentsMap, scanPosition );
}
for ( auto component : _components )
{
component->EndScan();
}

_componentsParallel.clear();
_componentsParallel.reserve( _components.size() );
for ( auto& componentsMapEntry : componentsMap )
{
_componentsParallel.insert( _componentsParallel.end(), componentsMapEntry.begin(), componentsMapEntry.end() );
}
_componentsParallel.clear();
_componentsParallel.reserve( _components.size() );
for ( auto& componentsMapEntry : componentsMap )
{
_componentsParallel.insert( _componentsParallel.end(), componentsMapEntry.begin(), componentsMapEntry.end() );
}

// clear _circuitDirty flag
Expand Down
4 changes: 2 additions & 2 deletions include/dspatch/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,10 @@ inline void Component::ScanParallel( std::vector<std::vector<DSPatch::Component*
}

// insert component at _scanPosition
if ( _scanPosition == (int)componentsMap.size() )
while ( (int)componentsMap.size() <= _scanPosition )
{
componentsMap.emplace_back( std::vector<DSPatch::Component*>{} );
componentsMap[_scanPosition].reserve( componentsMap.capacity() );
componentsMap.back().reserve( componentsMap.capacity() );
}
componentsMap[_scanPosition].emplace_back( this );
}
Expand Down

0 comments on commit d92d63b

Please sign in to comment.