diff --git a/src/BlockFile.cpp b/src/BlockFile.cpp index 636862344..671eadf62 100644 --- a/src/BlockFile.cpp +++ b/src/BlockFile.cpp @@ -193,13 +193,11 @@ void *BlockFile::CalcSummary(samplePtr buffer, size_t len, float *summary64K = (float *)(fullSummary.get() + mSummaryInfo.offset64K); float *summary256 = (float *)(fullSummary.get() + mSummaryInfo.offset256); - float *fbuffer = new float[len]; + Floats fbuffer{ len }; CopySamples(buffer, format, - (samplePtr)fbuffer, floatSample, len); + (samplePtr)fbuffer.get(), floatSample, len); - CalcSummaryFromBuffer(fbuffer, len, summary256, summary64K); - - delete[] fbuffer; + CalcSummaryFromBuffer(fbuffer.get(), len, summary256, summary64K); return fullSummary.get(); } @@ -436,14 +434,14 @@ bool BlockFile::Read256(float *buffer, { wxASSERT(start >= 0); - char *summary = new char[mSummaryInfo.totalSummaryBytes]; + ArrayOf summary{ mSummaryInfo.totalSummaryBytes }; // FIXME: TRAP_ERR ReadSummary() could return fail. - this->ReadSummary(summary); + this->ReadSummary(summary.get()); start = std::min( start, mSummaryInfo.frames256 ); len = std::min( len, mSummaryInfo.frames256 - start ); - CopySamples(summary + mSummaryInfo.offset256 + (start * mSummaryInfo.bytesPerFrame), + CopySamples(summary.get() + mSummaryInfo.offset256 + (start * mSummaryInfo.bytesPerFrame), mSummaryInfo.format, (samplePtr)buffer, floatSample, len * mSummaryInfo.fields); @@ -456,8 +454,6 @@ bool BlockFile::Read256(float *buffer, } } - delete[] summary; - return true; } @@ -475,14 +471,14 @@ bool BlockFile::Read64K(float *buffer, { wxASSERT(start >= 0); - char *summary = new char[mSummaryInfo.totalSummaryBytes]; + ArrayOf summary{ mSummaryInfo.totalSummaryBytes }; // FIXME: TRAP_ERR ReadSummary() could return fail. - this->ReadSummary(summary); + this->ReadSummary(summary.get()); start = std::min( start, mSummaryInfo.frames64K ); len = std::min( len, mSummaryInfo.frames64K - start ); - CopySamples(summary + mSummaryInfo.offset64K + + CopySamples(summary.get() + mSummaryInfo.offset64K + (start * mSummaryInfo.bytesPerFrame), mSummaryInfo.format, (samplePtr)buffer, floatSample, len*mSummaryInfo.fields); @@ -496,8 +492,6 @@ bool BlockFile::Read64K(float *buffer, } } - delete[] summary; - return true; } diff --git a/src/blockfile/LegacyBlockFile.cpp b/src/blockfile/LegacyBlockFile.cpp index e4d8f49f2..7df7422e0 100644 --- a/src/blockfile/LegacyBlockFile.cpp +++ b/src/blockfile/LegacyBlockFile.cpp @@ -68,7 +68,7 @@ void ComputeLegacySummaryInfo(const wxFileName &fileName, // 64K summary data // - float *summary = new float[info->frames64K * fields]; + Floats summary{ info->frames64K * fields }; SampleBuffer data(info->frames64K * fields, info->format); @@ -100,7 +100,7 @@ void ComputeLegacySummaryInfo(const wxFileName &fileName, int count = read / info->bytesPerFrame; CopySamples(data.ptr(), info->format, - (samplePtr)summary, floatSample, count); + (samplePtr)summary.get(), floatSample, count); (*min) = FLT_MAX; (*max) = FLT_MIN; @@ -118,8 +118,6 @@ void ComputeLegacySummaryInfo(const wxFileName &fileName, (*rms) = sqrt(sumsq / count); else (*rms) = 0; - - delete[] summary; } /// Construct a LegacyBlockFile memory structure that will point to an diff --git a/src/blockfile/ODDecodeBlockFile.cpp b/src/blockfile/ODDecodeBlockFile.cpp index 946d60f05..19ba836f0 100644 --- a/src/blockfile/ODDecodeBlockFile.cpp +++ b/src/blockfile/ODDecodeBlockFile.cpp @@ -369,9 +369,8 @@ void ODDecodeBlockFile::SetFileName(wxFileNameWrapper &&name) mFileName=std::move(name); /* mchinen oct 9 2009 don't think we need the char* but leaving it in for now just as a reminder that we might if wxFileName isn't threadsafe. - delete [] mFileNameChar; - mFileNameChar = new char[strlen(mFileName.GetFullPath().mb_str(wxConvUTF8))+1]; - strcpy(mFileNameChar,mFileName.GetFullPath().mb_str(wxConvUTF8)); */ + mFileNameChar.reinit(strlen(mFileName.GetFullPath().mb_str(wxConvUTF8))+1); + strcpy(mFileNameChar.get(), mFileName.GetFullPath().mb_str(wxConvUTF8)); */ mFileNameMutex.Unlock(); } @@ -409,6 +408,7 @@ void *ODDecodeBlockFile::CalcSummary(samplePtr buffer, size_t len, float *summary64K = (float *)(localFullSummary + mSummaryInfo.offset64K); float *summary256 = (float *)(localFullSummary + mSummaryInfo.offset256); + Floats floats; float *fbuffer; //mchinen: think we can hack this - don't allocate and copy if we don't need to., @@ -418,18 +418,14 @@ void *ODDecodeBlockFile::CalcSummary(samplePtr buffer, size_t len, } else { - fbuffer = new float[len]; + floats.reinit(len); + fbuffer = floats.get(); CopySamples(buffer, format, (samplePtr)fbuffer, floatSample, len); } BlockFile::CalcSummaryFromBuffer(fbuffer, len, summary256, summary64K); - //if we've used the float sample.. - if(format!=floatSample) - { - delete[] fbuffer; - } return localFullSummary; } diff --git a/src/blockfile/ODPCMAliasBlockFile.cpp b/src/blockfile/ODPCMAliasBlockFile.cpp index d91539b92..7db3039a8 100644 --- a/src/blockfile/ODPCMAliasBlockFile.cpp +++ b/src/blockfile/ODPCMAliasBlockFile.cpp @@ -379,24 +379,25 @@ void ODPCMAliasBlockFile::WriteSummary() // wxFFile summaryFile(mFileName.GetFullPath(), wxT("wb")); // ...and we use fopen instead. + FILE* summaryFile{}; wxString sFullPath = mFileName.GetFullPath(); - char* fileNameChar = new char[strlen(sFullPath.mb_str(wxConvFile)) + 1]; - strcpy(fileNameChar, sFullPath.mb_str(wxConvFile)); - FILE* summaryFile = fopen(fileNameChar, "wb"); + { + ArrayOf < char > fileNameChar{ strlen(sFullPath.mb_str(wxConvFile)) + 1 }; + strcpy(fileNameChar.get(), sFullPath.mb_str(wxConvFile)); + summaryFile = fopen(fileNameChar.get(), "wb"); - mFileNameMutex.Unlock(); + mFileNameMutex.Unlock(); - // JKC ANSWER-ME: Whay is IsOpened() commented out? - if( !summaryFile){//.IsOpened() ){ + // JKC ANSWER-ME: Whay is IsOpened() commented out? + if (!summaryFile){//.IsOpened() ){ - // Never silence the Log w.r.t write errors; they always count - //however, this is going to be called from a non-main thread, - //and wxLog calls are not thread safe. - printf("Unable to write summary data to file: %s", fileNameChar); - delete [] fileNameChar; - return; + // Never silence the Log w.r.t write errors; they always count + //however, this is going to be called from a non-main thread, + //and wxLog calls are not thread safe. + printf("Unable to write summary data to file: %s", fileNameChar.get()); + return; + } } - delete [] fileNameChar; // To build the summary data, call ReadData (implemented by the // derived classes) to get the sample data @@ -449,6 +450,7 @@ void *ODPCMAliasBlockFile::CalcSummary(samplePtr buffer, size_t len, float *summary64K = (float *)(localFullSummary + mSummaryInfo.offset64K); float *summary256 = (float *)(localFullSummary + mSummaryInfo.offset256); + Floats floats; float *fbuffer; //mchinen: think we can hack this - don't allocate and copy if we don't need to., @@ -458,18 +460,14 @@ void *ODPCMAliasBlockFile::CalcSummary(samplePtr buffer, size_t len, } else { - fbuffer = new float[len]; + floats.reinit(len); + fbuffer = floats.get(); CopySamples(buffer, format, (samplePtr)fbuffer, floatSample, len); } BlockFile::CalcSummaryFromBuffer(fbuffer, len, summary256, summary64K); - //if we've used the float sample.. - if(format!=floatSample) - { - delete[] fbuffer; - } return localFullSummary; } diff --git a/src/blockfile/SimpleBlockFile.cpp b/src/blockfile/SimpleBlockFile.cpp index 69d4b0a74..8dbca4b69 100644 --- a/src/blockfile/SimpleBlockFile.cpp +++ b/src/blockfile/SimpleBlockFile.cpp @@ -124,13 +124,13 @@ SimpleBlockFile::SimpleBlockFile(wxFileNameWrapper &&baseFileName, mCache.needWrite = true; mCache.format = format; const auto sampleDataSize = sampleLen * SAMPLE_SIZE(format); - mCache.sampleData = new char[sampleDataSize]; - memcpy(mCache.sampleData, sampleData, sampleDataSize); + mCache.sampleData.reinit(sampleDataSize); + memcpy(mCache.sampleData.get(), sampleData, sampleDataSize); ArrayOf cleanup; void* summaryData = BlockFile::CalcSummary(sampleData, sampleLen, - format, cleanup); - mCache.summaryData = new char[mSummaryInfo.totalSummaryBytes]; - memcpy(mCache.summaryData, summaryData, + format, cleanup); + mCache.summaryData.reinit(mSummaryInfo.totalSummaryBytes); + memcpy(mCache.summaryData.get(), summaryData, mSummaryInfo.totalSummaryBytes); } } @@ -155,11 +155,6 @@ SimpleBlockFile::SimpleBlockFile(wxFileNameWrapper &&existingFile, size_t len, SimpleBlockFile::~SimpleBlockFile() { - if (mCache.active) - { - delete[] mCache.sampleData; - delete[] (char *)mCache.summaryData; - } } bool SimpleBlockFile::WriteSimpleBlockFile( @@ -317,18 +312,18 @@ void SimpleBlockFile::FillCache() file.Close(); // Read samples into cache - mCache.sampleData = new char[mLen * SAMPLE_SIZE(mCache.format)]; - if (ReadData(mCache.sampleData, mCache.format, 0, mLen) != mLen) + mCache.sampleData.reinit(mLen * SAMPLE_SIZE(mCache.format)); + if (ReadData(mCache.sampleData.get(), mCache.format, 0, mLen) != mLen) { // Could not read all samples - delete mCache.sampleData; + mCache.sampleData.reset(); return; } // Read summary data into cache - mCache.summaryData = new char[mSummaryInfo.totalSummaryBytes]; - if (!ReadSummary(mCache.summaryData)) - memset(mCache.summaryData, 0, mSummaryInfo.totalSummaryBytes); + mCache.summaryData.reinit(mSummaryInfo.totalSummaryBytes); + if (!ReadSummary(mCache.summaryData.get())) + memset(mCache.summaryData.get(), 0, mSummaryInfo.totalSummaryBytes); // Cache is active but already on disk mCache.active = true; @@ -346,7 +341,7 @@ bool SimpleBlockFile::ReadSummary(void *data) if (mCache.active) { //wxLogDebug("SimpleBlockFile::ReadSummary(): Summary is already in cache."); - memcpy(data, mCache.summaryData, mSummaryInfo.totalSummaryBytes); + memcpy(data, mCache.summaryData.get(), mSummaryInfo.totalSummaryBytes); return true; } else @@ -398,7 +393,7 @@ size_t SimpleBlockFile::ReadData(samplePtr data, sampleFormat format, len = std::min(len, std::max(start, mLen) - start); CopySamples( - (samplePtr)(((char*)mCache.sampleData) + + (samplePtr)(mCache.sampleData.get() + start * SAMPLE_SIZE(mCache.format)), mCache.format, data, format, len); return len; @@ -540,7 +535,6 @@ auto SimpleBlockFile::GetSpaceUsage() const -> DiskByteCount void SimpleBlockFile::Recover(){ wxFFile file(mFileName.GetFullPath(), wxT("wb")); - //int i; if( !file.IsOpened() ){ // Can't do anything else. @@ -572,8 +566,8 @@ void SimpleBlockFile::WriteCacheToDisk() if (!GetNeedWriteCacheToDisk()) return; - if (WriteSimpleBlockFile(mCache.sampleData, mLen, mCache.format, - mCache.summaryData)) + if (WriteSimpleBlockFile(mCache.sampleData.get(), mLen, mCache.format, + mCache.summaryData.get())) mCache.needWrite = false; } diff --git a/src/blockfile/SimpleBlockFile.h b/src/blockfile/SimpleBlockFile.h index 05b8c9b0a..f733559c9 100644 --- a/src/blockfile/SimpleBlockFile.h +++ b/src/blockfile/SimpleBlockFile.h @@ -22,8 +22,9 @@ struct SimpleBlockFileCache { bool active; bool needWrite; sampleFormat format; - samplePtr sampleData; - void* summaryData; + ArrayOf sampleData, summaryData; + + SimpleBlockFileCache() {} }; // The AU formats we care about diff --git a/src/commands/CompareAudioCommand.cpp b/src/commands/CompareAudioCommand.cpp index 165cbeab3..593418897 100644 --- a/src/commands/CompareAudioCommand.cpp +++ b/src/commands/CompareAudioCommand.cpp @@ -99,8 +99,9 @@ bool CompareAudioCommand::Apply(CommandExecutionContext context) // Initialize buffers for track data to be analyzed auto buffSize = std::min(mTrack0->GetMaxBlockSize(), mTrack1->GetMaxBlockSize()); - float *buff0 = new float[buffSize]; - float *buff1 = new float[buffSize]; + + Floats buff0{ buffSize }; + Floats buff1{ buffSize }; // Compare tracks block by block auto s0 = mTrack0->TimeToLongSamples(mT0); @@ -113,8 +114,8 @@ bool CompareAudioCommand::Apply(CommandExecutionContext context) auto block = limitSampleBufferSize( mTrack0->GetBestBlockSize(position), s1 - position ); - mTrack0->Get((samplePtr)buff0, floatSample, position, block); - mTrack1->Get((samplePtr)buff1, floatSample, position, block); + mTrack0->Get((samplePtr)buff0.get(), floatSample, position, block); + mTrack1->Get((samplePtr)buff1.get(), floatSample, position, block); for (decltype(block) buffPos = 0; buffPos < block; ++buffPos) { @@ -131,9 +132,6 @@ bool CompareAudioCommand::Apply(CommandExecutionContext context) ); } - delete [] buff0; - delete [] buff1; - // Output the results double errorSeconds = mTrack0->LongSamplesToTime(errorCount); Status(wxString::Format(wxT("%li"), errorCount));