@@ -172,9 +172,13 @@ void SpikeSorter::updateSettings()
172172{
173173
174174 mut.enter ();
175+ sorterReady = false ;
175176 int numChannels = getNumInputs ();
176- if (numChannels > 0 )
177- overflowBuffer.setSize (getNumInputs (), overflowBufferSize);
177+ if (numChannels > 0 )
178+ {
179+ overflowBuffer.setSize (getNumInputs (), overflowBufferSize);
180+ overflowBuffer.clear ();
181+ }
178182
179183 if (channelBuffers != nullptr )
180184 delete channelBuffers;
@@ -191,7 +195,13 @@ void SpikeSorter::updateSettings()
191195 Array<const DataChannel*> chans;
192196 for (int c = 0 ; c < nChans; c++)
193197 {
194- chans.add (getDataChannel (elec->channels [c]));
198+ const DataChannel* ch = getDataChannel (elec->channels [c]);
199+ if (!ch)
200+ {
201+ // not enough channels for the electrodes
202+ return ;
203+ }
204+ chans.add (ch);
195205 }
196206
197207 SpikeChannel* spk = new SpikeChannel (SpikeChannel::typeFromNumChannels (nChans), this , chans);
@@ -200,7 +210,7 @@ void SpikeSorter::updateSettings()
200210
201211 spikeChannelArray.add (spk);
202212 }
203-
213+ sorterReady = true ;
204214 mut.exit ();
205215}
206216
@@ -212,9 +222,6 @@ Electrode::~Electrode()
212222 delete[] voltageScale;
213223 delete[] channels;
214224 delete[] runningStats;
215-
216- delete spikeSort;
217-
218225}
219226
220227Electrode::Electrode (int ID, UniqueIDgenerator* uniqueIDgenerator_, PCAcomputingThread* pth, String _name, int _numChannels, int * _channels, float default_threshold, int pre , int post , float samplingRate , int sourceId, int subIdx)
@@ -459,6 +466,7 @@ bool SpikeSorter::addElectrode(int nChans, String name, double Depth)
459466 eventlog += String (chans[k])+ " " + name;
460467
461468 // addNetworkEventToQueue(StringTS(eventlog));
469+ delete[] chans;
462470
463471 resetElectrode (newElectrode);
464472 electrodes.add (newElectrode);
@@ -647,6 +655,12 @@ bool SpikeSorter::enable()
647655{
648656
649657 useOverflowBuffer.clear ();
658+ if (!sorterReady)
659+ {
660+ CoreServices::sendStatusMessage (" Not enough channels for the configured electrodes" );
661+ std::cout << " SpikeSorter: Not enough channels for the configured electrodes" << std::endl;
662+ return false ;
663+ }
650664
651665 for (int i = 0 ; i < electrodes.size (); i++)
652666 useOverflowBuffer.add (false );
@@ -1036,7 +1050,7 @@ float SpikeSorter::getNextSample(int& chan)
10361050 // useOverflowBuffer = false;
10371051 // std::cout << " sample index " << sampleIndex << "from regular buffer" << std::endl;
10381052
1039- if (sampleIndex < dataBuffer-> getNumSamples ())
1053+ if (sampleIndex < getNumSamples (chan ))
10401054 return (*dataBuffer->getReadPointer (chan, sampleIndex));
10411055 else
10421056 return 0 ;
@@ -1112,7 +1126,7 @@ void SpikeSorter::addProbes(String probeType,int numProbes, int nElectrodesPerPr
11121126 increaseUniqueProbeID (probeType);
11131127 }
11141128}
1115- Array <Electrode*> SpikeSorter::getElectrodes ()
1129+ const OwnedArray <Electrode>& SpikeSorter::getElectrodes ()
11161130{
11171131 return electrodes;
11181132}
@@ -1292,6 +1306,9 @@ void SpikeSorter::loadCustomParametersFromXml()
12921306 newElectrode->thresholds [k] = thres[k];
12931307 newElectrode->isActive [k] = isActive[k];
12941308 }
1309+ delete[] channels;
1310+ delete[] thres;
1311+ delete[] isActive;
12951312
12961313 newElectrode->advancerID = advancerID;
12971314 newElectrode->depthOffsetMM = depthOffsetMM;
0 commit comments