Skip to content

Commit 3f4aa3c

Browse files
committed
Fix crashes
1 parent 2eb3cdf commit 3f4aa3c

9 files changed

Lines changed: 41 additions & 27 deletions

File tree

Source/Plugins/KWIKFormat/RecordEngine/HDF5FileFormat.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -893,18 +893,17 @@ KWXFile::KWXFile(String basename) : HDF5FileBase()
893893
{
894894
initFile(basename);
895895
numElectrodes=0;
896-
transformVector = new int16[MAX_TRANSFORM_SIZE];
896+
transformVector.malloc(MAX_TRANSFORM_SIZE);
897897
}
898898

899899
KWXFile::KWXFile() : HDF5FileBase()
900900
{
901901
numElectrodes=0;
902-
transformVector = new int16[MAX_TRANSFORM_SIZE];
902+
transformVector.malloc(MAX_TRANSFORM_SIZE);
903903
}
904904

905905
KWXFile::~KWXFile()
906906
{
907-
delete transformVector;
908907
}
909908

910909
String KWXFile::getFileName()

Source/Plugins/KWIKFormat/RecordEngine/HDF5FileFormat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ class KWXFile : public HDF5FileBase
205205
OwnedArray<HDF5RecordingData> timeStamps;
206206
Array<int> channelArray;
207207
int numElectrodes;
208-
int16* transformVector;
208+
HeapBlock<int16> transformVector;
209+
//int16* transformVector;
209210

210211
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(KWXFile);
211212
};

Source/Plugins/KWIKFormat/RecordEngine/HDF5Recording.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,19 @@
2222
*/
2323

2424
#include "HDF5Recording.h"
25-
#define MAX_BUFFER_SIZE 10000
25+
#define MAX_BUFFER_SIZE 40960
2626
#define CHANNEL_TIMESTAMP_PREALLOC_SIZE 16
2727
#define TIMESTAMP_EACH_NSAMPLES 1024
2828

29-
HDF5Recording::HDF5Recording() : processorIndex(-1), hasAcquired(false)
29+
HDF5Recording::HDF5Recording() : processorIndex(-1), hasAcquired(false), bufferSize(MAX_BUFFER_SIZE)
3030
{
3131
//timestamp = 0;
32-
scaledBuffer = new float[MAX_BUFFER_SIZE];
33-
intBuffer = new int16[MAX_BUFFER_SIZE];
32+
scaledBuffer.malloc(MAX_BUFFER_SIZE);
33+
intBuffer.malloc(MAX_BUFFER_SIZE);
3434
}
3535

3636
HDF5Recording::~HDF5Recording()
37-
{
38-
delete scaledBuffer;
39-
delete intBuffer;
37+
{
4038
}
4139

4240
String HDF5Recording::getEngineID() const
@@ -67,6 +65,9 @@ void HDF5Recording::registerProcessor(const GenericProcessor* proc)
6765

6866
void HDF5Recording::resetChannels()
6967
{
68+
scaledBuffer.malloc(MAX_BUFFER_SIZE);
69+
intBuffer.malloc(MAX_BUFFER_SIZE);
70+
bufferSize = MAX_BUFFER_SIZE;
7071
processorIndex = -1;
7172
fileArray.clear();
7273
channelsPerProcessor.clear();
@@ -173,6 +174,7 @@ void HDF5Recording::closeFiles()
173174
{
174175
if (fileArray[i]->isOpen())
175176
{
177+
std::cout << "Closed file " << i << std::endl;
176178
fileArray[i]->stopRecording();
177179
fileArray[i]->close();
178180
bitVoltsArray[i]->clear();
@@ -196,11 +198,18 @@ void HDF5Recording::startChannelBlock()
196198

197199
void HDF5Recording::writeData(int writeChannel, int realChannel, const float* buffer, int size)
198200
{
201+
if (size > bufferSize) //Shouldn't happen, and if it happens it'll be slow, but better this than crashing. Will be reset on reset.
202+
{
203+
std::cerr << "Write buffer overrun, resizing to" << size << std::endl;
204+
bufferSize = size;
205+
scaledBuffer.malloc(size);
206+
intBuffer.malloc(size);
207+
}
199208
double multFactor = 1 / (float(0x7fff) * getChannel(realChannel)->bitVolts);
200209
int index = processorMap[getChannel(realChannel)->recordIndex];
201-
FloatVectorOperations::copyWithMultiply(scaledBuffer, buffer, multFactor, size);
202-
AudioDataConverters::convertFloatToInt16LE(scaledBuffer, intBuffer, size);
203-
fileArray[index]->writeRowData(intBuffer, size, recordedChanToKWDChan[writeChannel]);
210+
FloatVectorOperations::copyWithMultiply(scaledBuffer.getData(), buffer, multFactor, size);
211+
AudioDataConverters::convertFloatToInt16LE(scaledBuffer.getData(), intBuffer.getData(), size);
212+
fileArray[index]->writeRowData(intBuffer.getData(), size, recordedChanToKWDChan[writeChannel]);
204213

205214
int sampleOffset = channelLeftOverSamples[writeChannel];
206215
int blockStart = sampleOffset;

Source/Plugins/KWIKFormat/RecordEngine/HDF5Recording.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ class HDF5Recording : public RecordEngine
6262
OwnedArray<HDF5RecordingInfo> infoArray;
6363
ScopedPointer<KWEFile> eventFile;
6464
ScopedPointer<KWXFile> spikesFile;
65-
float* scaledBuffer;
66-
int16* intBuffer;
65+
HeapBlock<float> scaledBuffer;
66+
HeapBlock<int16> intBuffer;
67+
int bufferSize;
68+
//float* scaledBuffer;
69+
//int16* intBuffer;
6770

6871
bool hasAcquired;
6972

Source/Processors/DataThreads/RhythmNode/RHD2000Thread.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
#define REGISTER_59_MISO_B 58
4545
#define RHD2132_16CH_OFFSET 8
4646

47-
//#define DEBUG_EMULATE_HEADSTAGES 8
48-
//#define DEBUG_EMULATE_64CH
47+
#define DEBUG_EMULATE_HEADSTAGES 8
48+
#define DEBUG_EMULATE_64CH
4949

5050
#define INIT_STEP ( evalBoard->isUSB3() ? 256 : 60)
5151

@@ -188,10 +188,10 @@ RHD2000Thread::~RHD2000Thread()
188188

189189
//deleteAndZero(dataBlock);
190190

191-
delete dacStream;
192-
delete dacChannels;
193-
delete dacThresholds;
194-
delete dacChannelsToUpdate;
191+
delete[] dacStream;
192+
delete[] dacChannels;
193+
delete[] dacThresholds;
194+
delete[] dacChannelsToUpdate;
195195

196196
}
197197

Source/Processors/GenericProcessor/GenericProcessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ void GenericProcessor::setTimestamp(MidiBuffer& events, int64 timestamp)
632632
0,
633633
0,
634634
0,
635-
data.length() + 1, //It doesn't hurt to send the end-string null and can help avoid issues
635+
data.sizeInBytes(), //It doesn't hurt to send the end-string null and can help avoid issues
636636
(uint8*)data.getAddress(),
637637
true);
638638

Source/Processors/MessageCenter/MessageCenter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void MessageCenter::process(AudioSampleBuffer& buffer, MidiBuffer& eventBuffer)
134134
0,
135135
0,
136136
0,
137-
data.length() + 1, //It doesn't hurt to send the end-string null and can help avoid issues
137+
data.sizeInBytes(), //It doesn't hurt to send the end-string null and can help avoid issues
138138
(uint8*)data.getAddress());
139139

140140
needsToSendTimestampMessage = false;
@@ -153,7 +153,7 @@ void MessageCenter::process(AudioSampleBuffer& buffer, MidiBuffer& eventBuffer)
153153
0,
154154
0,
155155
0,
156-
data.length()+1, //It doesn't hurt to send the end-string null and can help avoid issues
156+
data.sizeInBytes(), //It doesn't hurt to send the end-string null and can help avoid issues
157157
(uint8*) data.getAddress());
158158

159159
newEventAvailable = false;

Source/Processors/RecordNode/RecordNode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ void RecordNode::setParameter(int parameterIndex, float newValue)
334334
{
335335

336336

337-
// std::cout << "STOP RECORDING." << std::endl;
337+
std::cout << "STOP RECORDING." << std::endl;
338338

339339
if (isRecording)
340340
{
@@ -345,7 +345,7 @@ void RecordNode::setParameter(int parameterIndex, float newValue)
345345
m_recordThread->waitForThreadToExit(2000);
346346
while (m_recordThread->isThreadRunning())
347347
{
348-
348+
std::cerr << "RecordEngine timeout" << std::endl;
349349
if (AlertWindow::showOkCancelBox(AlertWindow::WarningIcon, "Record Thread timeout",
350350
"The recording thread is taking too long to close.\nThis could mean there is still data waiting to be written in the buffer, but it normally "
351351
"shouldn't take this long.\nYou can either wait a bit more or forcefully close the thread. Note that data might be lost or corrupted"

Source/Processors/RecordNode/RecordThread.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,13 @@ void RecordThread::run()
9696
{
9797
writeData(dataBuffer, BLOCK_MAX_WRITE_SAMPLES, BLOCK_MAX_WRITE_EVENTS, BLOCK_MAX_WRITE_SPIKES);
9898
}
99+
std::cout << "Exiting record thread" << std::endl;
99100
//4-Before closing the thread, try to write the remaining samples
100101
if (!closeEarly)
101102
{
102103
writeData(dataBuffer, -1, -1, -1);
103104

105+
std::cout << "Closing files" << std::endl;
104106
//5-Close files
105107
EVERY_ENGINE->closeFiles();
106108
}

0 commit comments

Comments
 (0)