diff --git a/doc/developer-notes.md b/doc/developer-notes.md --- a/doc/developer-notes.md +++ b/doc/developer-notes.md @@ -464,6 +464,10 @@ - *Rationale*: This avoids memory and resource leaks, and ensures exception safety +- Use `std::make_unique()` to construct objects owned by `unique_ptr`s + + - *Rationale*: `std::make_unique` is concise and ensures exception safety in complex expressions. + C++ data structures -------------------- diff --git a/src/bench/bench_bitcoin.cpp b/src/bench/bench_bitcoin.cpp --- a/src/bench/bench_bitcoin.cpp +++ b/src/bench/bench_bitcoin.cpp @@ -110,8 +110,8 @@ return EXIT_FAILURE; } - std::unique_ptr printer( - new benchmark::ConsolePrinter()); + std::unique_ptr printer = + std::make_unique(); std::string printer_arg = gArgs.GetArg("-printer", DEFAULT_BENCH_PRINTER); if ("plot" == printer_arg) { printer.reset(new benchmark::PlotlyPrinter( diff --git a/src/test/allocator_tests.cpp b/src/test/allocator_tests.cpp --- a/src/test/allocator_tests.cpp +++ b/src/test/allocator_tests.cpp @@ -171,7 +171,7 @@ BOOST_AUTO_TEST_CASE(lockedpool_tests_mock) { // Test over three virtual arenas, of which one will succeed being locked - std::unique_ptr x(new TestLockedPageAllocator(3, 1)); + auto x = std::make_unique(3, 1); LockedPool pool(std::move(x)); BOOST_CHECK(pool.stats().total == 0); BOOST_CHECK(pool.stats().locked == 0); diff --git a/src/test/checkqueue_tests.cpp b/src/test/checkqueue_tests.cpp --- a/src/test/checkqueue_tests.cpp +++ b/src/test/checkqueue_tests.cpp @@ -127,8 +127,7 @@ * with each specified size_t Checks pushed. */ static void Correct_Queue_range(std::vector range) { - auto small_queue = - std::unique_ptr(new Correct_Queue{QUEUE_BATCH_SIZE}); + auto small_queue = std::make_unique(QUEUE_BATCH_SIZE); boost::thread_group tg; for (auto x = 0; x < nScriptCheckThreads; ++x) { tg.create_thread([&] { small_queue->Thread(); }); @@ -189,8 +188,7 @@ /** Test that failing checks are caught */ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure) { - auto fail_queue = - std::unique_ptr(new Failing_Queue{QUEUE_BATCH_SIZE}); + auto fail_queue = std::make_unique(QUEUE_BATCH_SIZE); boost::thread_group tg; for (auto x = 0; x < nScriptCheckThreads; ++x) { @@ -223,8 +221,7 @@ // Test that a block validation which fails does not interfere with // future blocks, ie, the bad state is cleared. BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure) { - auto fail_queue = - std::unique_ptr(new Failing_Queue{QUEUE_BATCH_SIZE}); + auto fail_queue = std::make_unique(QUEUE_BATCH_SIZE); boost::thread_group tg; for (auto x = 0; x < nScriptCheckThreads; ++x) { tg.create_thread([&] { fail_queue->Thread(); }); @@ -251,8 +248,7 @@ // just one check being called repeatedly. Test that checks are not called // more than once as well BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck) { - auto queue = - std::unique_ptr(new Unique_Queue{QUEUE_BATCH_SIZE}); + auto queue = std::make_unique(QUEUE_BATCH_SIZE); boost::thread_group tg; for (auto x = 0; x < nScriptCheckThreads; ++x) { tg.create_thread([&] { queue->Thread(); }); @@ -288,8 +284,7 @@ // checks might mean leaving a check un-swapped out, and decreasing by 1 each // time could leave the data hanging across a sequence of blocks. BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory) { - auto queue = - std::unique_ptr(new Memory_Queue{QUEUE_BATCH_SIZE}); + auto queue = std::make_unique(QUEUE_BATCH_SIZE); boost::thread_group tg; for (auto x = 0; x < nScriptCheckThreads; ++x) { tg.create_thread([&] { queue->Thread(); }); @@ -320,8 +315,7 @@ // Test that a new verification cannot occur until all checks // have been destructed BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup) { - auto queue = std::unique_ptr( - new FrozenCleanup_Queue{QUEUE_BATCH_SIZE}); + auto queue = std::make_unique(QUEUE_BATCH_SIZE); boost::thread_group tg; bool fails = false; for (auto x = 0; x < nScriptCheckThreads; ++x) { @@ -364,8 +358,7 @@ /** Test that CCheckQueueControl is threadsafe */ BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks) { - auto queue = - std::unique_ptr(new Standard_Queue{QUEUE_BATCH_SIZE}); + auto queue = std::make_unique(QUEUE_BATCH_SIZE); { boost::thread_group tg; std::atomic nThreads{0}; diff --git a/src/test/test_bitcoin_fuzzy.cpp b/src/test/test_bitcoin_fuzzy.cpp --- a/src/test/test_bitcoin_fuzzy.cpp +++ b/src/test/test_bitcoin_fuzzy.cpp @@ -281,8 +281,7 @@ static std::unique_ptr globalVerifyHandle; void initialize() { - globalVerifyHandle = - std::unique_ptr(new ECCVerifyHandle()); + globalVerifyHandle = std::make_unique(); } // This function is used by libFuzzer diff --git a/src/wallet/test/coinselector_tests.cpp b/src/wallet/test/coinselector_tests.cpp --- a/src/wallet/test/coinselector_tests.cpp +++ b/src/wallet/test/coinselector_tests.cpp @@ -72,8 +72,8 @@ // fake out IsFromMe() tx.vin.resize(1); } - std::unique_ptr wtx( - new CWalletTx(&wallet, MakeTransactionRef(std::move(tx)))); + auto wtx = + std::make_unique(&wallet, MakeTransactionRef(std::move(tx))); if (fIsFromMe) { wtx->fDebitCached = true; wtx->nDebitCached = SATOSHI;