diff options
| author | Carl Hetherington <cth@carlh.net> | 2021-10-11 19:55:06 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2021-10-11 20:13:21 +0200 |
| commit | 44b69f2d9affb048c3d166e3a62bf3462dd5c8b5 (patch) | |
| tree | 7cf2a540d01c66f9a7d12acfdabd0ee2f4251c80 /src | |
| parent | 805d4a48fa6e4d8e28fd582a2ae6ba78b8343144 (diff) | |
Replace some raw arrays with std::vectors.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/cross_windows.cc | 5 | ||||
| -rw-r--r-- | src/lib/ext.cc | 20 | ||||
| -rw-r--r-- | src/lib/ffmpeg_encoder.cc | 7 | ||||
| -rw-r--r-- | src/lib/file_log.cc | 11 | ||||
| -rw-r--r-- | src/lib/image_examiner.cc | 8 | ||||
| -rw-r--r-- | src/lib/update_checker.cc | 7 | ||||
| -rw-r--r-- | src/lib/update_checker.h | 2 | ||||
| -rw-r--r-- | src/tools/dcpomatic.cc | 5 | ||||
| -rw-r--r-- | src/tools/dcpomatic_player.cc | 5 | ||||
| -rw-r--r-- | src/wx/gl_video_view.cc | 14 |
10 files changed, 34 insertions, 50 deletions
diff --git a/src/lib/cross_windows.cc b/src/lib/cross_windows.cc index b3d9a1558..01bbe521d 100644 --- a/src/lib/cross_windows.cc +++ b/src/lib/cross_windows.cc @@ -355,10 +355,9 @@ static string wchar_to_utf8 (wchar_t const * s) { int const length = (wcslen(s) + 1) * 2; - char* utf8 = new char[length]; - WideCharToMultiByte (CP_UTF8, 0, s, -1, utf8, length, 0, 0); + std::vector<char> utf8(length); + WideCharToMultiByte (CP_UTF8, 0, s, -1, utf8.data(), length, 0, 0); string u (utf8); - delete[] utf8; return u; } diff --git a/src/lib/ext.cc b/src/lib/ext.cc index feba68c1f..863cc0307 100644 --- a/src/lib/ext.cc +++ b/src/lib/ext.cc @@ -106,7 +106,7 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total throw CopyError (String::compose("Failed to open file %1", from.string()), 0); } - uint8_t* buffer = new uint8_t[block_size]; + std::vector<uint8_t> buffer(block_size); Digester digester; int progress_frequency = 1; @@ -114,28 +114,25 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total uint64_t remaining = file_size (from); while (remaining > 0) { uint64_t const this_time = min(remaining, block_size); - size_t read = fread (buffer, 1, this_time, in); + size_t read = fread (buffer.data(), 1, this_time, in); if (read != this_time) { fclose (in); ext4_fclose (&out); - delete[] buffer; throw CopyError (String::compose("Short read; expected %1 but read %2", this_time, read), 0); } - digester.add (buffer, this_time); + digester.add (buffer.data(), this_time); size_t written; - r = ext4_fwrite (&out, buffer, this_time, &written); + r = ext4_fwrite (&out, buffer.data(), this_time, &written); if (r != EOK) { fclose (in); ext4_fclose (&out); - delete[] buffer; throw CopyError ("Write failed", r); } if (written != this_time) { fclose (in); ext4_fclose (&out); - delete[] buffer; throw CopyError (String::compose("Short write; expected %1 but wrote %2", this_time, written), 0); } remaining -= this_time; @@ -149,7 +146,6 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total fclose (in); ext4_fclose (&out); - delete[] buffer; return digester.get (); } @@ -167,21 +163,20 @@ read (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_ } LOG_DISK("Opened %1 for read", to.generic_string()); - uint8_t* buffer = new uint8_t[block_size]; + std::vector<uint8_t> buffer(block_size); Digester digester; uint64_t remaining = file_size (from); while (remaining > 0) { uint64_t const this_time = min(remaining, block_size); size_t read; - r = ext4_fread (&in, buffer, this_time, &read); + r = ext4_fread (&in, buffer.data(), this_time, &read); if (read != this_time) { ext4_fclose (&in); - delete[] buffer; throw VerifyError (String::compose("Short read; expected %1 but read %2", this_time, read), 0); } - digester.add (buffer, this_time); + digester.add (buffer.data(), this_time); remaining -= this_time; total_remaining -= this_time; if (nanomsg) { @@ -190,7 +185,6 @@ read (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_ } ext4_fclose (&in); - delete[] buffer; return digester.get (); } diff --git a/src/lib/ffmpeg_encoder.cc b/src/lib/ffmpeg_encoder.cc index dd773168a..e3a37677b 100644 --- a/src/lib/ffmpeg_encoder.cc +++ b/src/lib/ffmpeg_encoder.cc @@ -161,7 +161,7 @@ FFmpegEncoder::go () auto const video_frame = DCPTime::from_frames (1, _film->video_frame_rate ()); int const audio_frames = video_frame.frames_round(_film->audio_frame_rate()); - float* interleaved = new float[_output_audio_channels * audio_frames]; + std::vector<float> interleaved(_output_audio_channels * audio_frames); auto deinterleaved = make_shared<AudioBuffers>(_output_audio_channels, audio_frames); int const gets_per_frame = _film->three_d() ? 2 : 1; for (DCPTime i; i < _film->length(); i += video_frame) { @@ -201,9 +201,9 @@ FFmpegEncoder::go () waker.nudge (); - _butler->get_audio (interleaved, audio_frames); + _butler->get_audio (interleaved.data(), audio_frames); /* XXX: inefficient; butler interleaves and we deinterleave again */ - float* p = interleaved; + float* p = interleaved.data(); for (int j = 0; j < audio_frames; ++j) { for (int k = 0; k < _output_audio_channels; ++k) { deinterleaved->data(k)[j] = *p++; @@ -211,7 +211,6 @@ FFmpegEncoder::go () } encoder->audio (deinterleaved); } - delete[] interleaved; for (auto i: file_encoders) { i.flush (); diff --git a/src/lib/file_log.cc b/src/lib/file_log.cc index 5cc9c5569..246641cc9 100644 --- a/src/lib/file_log.cc +++ b/src/lib/file_log.cc @@ -83,23 +83,22 @@ FileLog::head_and_tail (int amount) const string out; - auto buffer = new char[max(head_amount, tail_amount) + 1]; + std::vector<char> buffer(max(head_amount, tail_amount) + 1); - int N = fread (buffer, 1, head_amount, f); + int N = fread (buffer.data(), 1, head_amount, f); buffer[N] = '\0'; - out += string (buffer); + out += string (buffer.data()); if (tail_amount > 0) { out += "\n .\n .\n .\n"; fseek (f, - tail_amount - 1, SEEK_END); - N = fread (buffer, 1, tail_amount, f); + N = fread (buffer.data(), 1, tail_amount, f); buffer[N] = '\0'; - out += string (buffer) + "\n"; + out += string (buffer.data()) + "\n"; } - delete[] buffer; fclose (f); return out; diff --git a/src/lib/image_examiner.cc b/src/lib/image_examiner.cc index ae12d7adb..562a7c557 100644 --- a/src/lib/image_examiner.cc +++ b/src/lib/image_examiner.cc @@ -55,16 +55,14 @@ ImageExaminer::ImageExaminer (shared_ptr<const Film> film, shared_ptr<const Imag if (!f) { throw FileError ("Could not open file for reading", path); } - auto buffer = new uint8_t[size]; - checked_fread (buffer, size, f, path); + std::vector<uint8_t> buffer(size); + checked_fread (buffer.data(), size, f, path); fclose (f); try { - _video_size = dcp::decompress_j2k (buffer, size, 0)->size (); + _video_size = dcp::decompress_j2k(buffer.data(), size, 0)->size(); } catch (dcp::ReadError& e) { - delete[] buffer; throw DecodeError (String::compose (_("Could not decode JPEG2000 file %1 (%2)"), path, e.what ())); } - delete[] buffer; } else { FFmpegImageProxy proxy(content->path(0)); _video_size = proxy.image(Image::Alignment::COMPACT).image->size(); diff --git a/src/lib/update_checker.cc b/src/lib/update_checker.cc index 3bc02b50a..2c2e23be1 100644 --- a/src/lib/update_checker.cc +++ b/src/lib/update_checker.cc @@ -57,7 +57,7 @@ write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user) * do the work. */ UpdateChecker::UpdateChecker () - : _buffer (new char[BUFFER_SIZE]) + : _buffer (BUFFER_SIZE) , _state (State::NOT_RUN) { _curl = curl_easy_init (); @@ -97,7 +97,6 @@ UpdateChecker::~UpdateChecker () } catch (...) {} curl_easy_cleanup (_curl); - delete[] _buffer; } @@ -142,7 +141,7 @@ UpdateChecker::thread () /* Parse the reply */ _buffer[_offset] = '\0'; - string s (_buffer); + string s (_buffer.data()); cxml::Document doc ("Update"); doc.read_string (s); @@ -181,7 +180,7 @@ size_t UpdateChecker::write_callback (void* data, size_t size, size_t nmemb) { size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1)); - memcpy (_buffer + _offset, data, t); + memcpy (_buffer.data() + _offset, data, t); _offset += t; return t; } diff --git a/src/lib/update_checker.h b/src/lib/update_checker.h index aa5f620e6..53ef4457b 100644 --- a/src/lib/update_checker.h +++ b/src/lib/update_checker.h @@ -89,7 +89,7 @@ private: void set_state (State); void thread (); - char* _buffer; + std::vector<char> _buffer; int _offset = 0; CURL* _curl = nullptr; diff --git a/src/tools/dcpomatic.cc b/src/tools/dcpomatic.cc index 0b7f632e1..2cc3187d6 100644 --- a/src/tools/dcpomatic.cc +++ b/src/tools/dcpomatic.cc @@ -387,7 +387,7 @@ public: #else int accelerators = 6; #endif - auto accel = new wxAcceleratorEntry[accelerators]; + std::vector<wxAcceleratorEntry> accel(accelerators); /* [Shortcut] Ctrl+A:Add file(s) to the film */ accel[0].Set (wxACCEL_CTRL, static_cast<int>('A'), ID_add_file); /* [Shortcut] Delete:Remove selected content from film */ @@ -409,9 +409,8 @@ public: Bind (wxEVT_MENU, boost::bind (&DOMFrame::timeline_pressed, this), ID_timeline); Bind (wxEVT_MENU, boost::bind (&DOMFrame::back_frame, this), ID_back_frame); Bind (wxEVT_MENU, boost::bind (&DOMFrame::forward_frame, this), ID_forward_frame); - wxAcceleratorTable accel_table (accelerators, accel); + wxAcceleratorTable accel_table (accelerators, accel.data()); SetAcceleratorTable (accel_table); - delete[] accel; } void remove_accelerators () diff --git a/src/tools/dcpomatic_player.cc b/src/tools/dcpomatic_player.cc index e409b9731..96a22a87f 100644 --- a/src/tools/dcpomatic_player.cc +++ b/src/tools/dcpomatic_player.cc @@ -212,7 +212,7 @@ public: _stress.setup (this, _controls); - wxAcceleratorEntry* accel = new wxAcceleratorEntry[accelerators]; + std::vector<wxAcceleratorEntry> accel(accelerators); accel[0].Set(wxACCEL_NORMAL, WXK_SPACE, ID_start_stop); accel[1].Set(wxACCEL_NORMAL, WXK_LEFT, ID_go_back_frame); accel[2].Set(wxACCEL_NORMAL, WXK_RIGHT, ID_go_forward_frame); @@ -227,9 +227,8 @@ public: #ifdef __WXOSX__ accel[11].Set(wxACCEL_CTRL, static_cast<int>('W'), ID_file_close); #endif - wxAcceleratorTable accel_table (accelerators, accel); + wxAcceleratorTable accel_table (accelerators, accel.data()); SetAcceleratorTable (accel_table); - delete[] accel; Bind (wxEVT_MENU, boost::bind(&DOMFrame::start_stop_pressed, this), ID_start_stop); Bind (wxEVT_MENU, boost::bind(&DOMFrame::go_back_frame, this), ID_go_back_frame); diff --git a/src/wx/gl_video_view.cc b/src/wx/gl_video_view.cc index 2f5b27da8..3ce0c4d7b 100644 --- a/src/wx/gl_video_view.cc +++ b/src/wx/gl_video_view.cc @@ -376,10 +376,9 @@ GLVideoView::setup_shaders () glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length); string log; if (log_length > 0) { - char* log_char = new char[log_length]; - glGetShaderInfoLog(shader, log_length, nullptr, log_char); - log = string(log_char); - delete[] log_char; + std::vector<char> log_char(log_length); + glGetShaderInfoLog(shader, log_length, nullptr, log_char.data()); + log = string(log_char.data()); } glDeleteShader(shader); throw GLError(String::compose("Could not compile shader (%1)", log).c_str(), -1); @@ -405,10 +404,9 @@ GLVideoView::setup_shaders () glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length); string log; if (log_length > 0) { - char* log_char = new char[log_length]; - glGetProgramInfoLog(program, log_length, nullptr, log_char); - log = string(log_char); - delete[] log_char; + std::vector<char> log_char(log_length); + glGetProgramInfoLog(program, log_length, nullptr, log_char.data()); + log = string(log_char.data()); } glDeleteProgram (program); throw GLError(String::compose("Could not link shader (%1)", log).c_str(), -1); |
