diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index 778f41055..aaafe746c 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -1357,22 +1357,22 @@ universal_notifier_t::notifier_strategy_t universal_notifier_t::resolve_default_ } universal_notifier_t &universal_notifier_t::default_notifier() { - static universal_notifier_t *result = + static std::unique_ptr result = new_notifier_for_strategy(universal_notifier_t::resolve_default_strategy()); return *result; } -universal_notifier_t *universal_notifier_t::new_notifier_for_strategy( +std::unique_ptr universal_notifier_t::new_notifier_for_strategy( universal_notifier_t::notifier_strategy_t strat, const wchar_t *test_path) { switch (strat) { case strategy_notifyd: { - return new universal_notifier_notifyd_t(); + return make_unique(); } case strategy_shmem_polling: { - return new universal_notifier_shmem_poller_t(); + return make_unique(); } case strategy_named_pipe: { - return new universal_notifier_named_pipe_t(test_path); + return make_unique(test_path); } default: { debug(0, "Unsupported universal notifier strategy %d\n", strat); diff --git a/src/env_universal_common.h b/src/env_universal_common.h index c25c7e2e6..a410f92c3 100644 --- a/src/env_universal_common.h +++ b/src/env_universal_common.h @@ -135,9 +135,9 @@ class universal_notifier_t { static notifier_strategy_t resolve_default_strategy(); virtual ~universal_notifier_t(); - // Factory constructor. Free with delete. - static universal_notifier_t *new_notifier_for_strategy(notifier_strategy_t strat, - const wchar_t *test_path = NULL); + // Factory constructor. + static std::unique_ptr new_notifier_for_strategy(notifier_strategy_t strat, + const wchar_t *test_path = NULL); // Default instance. Other instances are possible for testing. static universal_notifier_t &default_notifier(); diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 6fb542841..5454b352b 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -532,12 +532,12 @@ static int test_iothread_thread_call(int *addr) { static void test_iothread(void) { say(L"Testing iothreads"); - int *int_ptr = new int(0); + std::unique_ptr int_ptr = make_unique(0); int iterations = 50000; int max_achieved_thread_count = 0; double start = timef(); for (int i = 0; i < iterations; i++) { - int thread_count = iothread_perform(test_iothread_thread_call, int_ptr); + int thread_count = iothread_perform(test_iothread_thread_call, int_ptr.get()); max_achieved_thread_count = std::max(max_achieved_thread_count, thread_count); } @@ -552,8 +552,6 @@ static void test_iothread(void) { say(L" (%.02f msec, with max of %d threads)", (end - start) * 1000.0, max_achieved_thread_count); - - delete int_ptr; } static parser_test_error_bits_t detect_argument_errors(const wcstring &src) { @@ -2553,7 +2551,7 @@ static void test_universal_callbacks() { if (system("rm -Rf /tmp/fish_uvars_test")) err(L"rm failed"); } -bool poll_notifier(universal_notifier_t *note) { +bool poll_notifier(const std::unique_ptr ¬e) { bool result = false; if (note->usec_delay_between_polls() > 0) { result = note->poll(); @@ -2591,7 +2589,7 @@ static void trigger_or_wait_for_notification(universal_notifier_t::notifier_stra static void test_notifiers_with_strategy(universal_notifier_t::notifier_strategy_t strategy) { say(L"Testing universal notifiers with strategy %d", (int)strategy); - universal_notifier_t *notifiers[16]; + std::unique_ptr notifiers[16]; size_t notifier_count = sizeof notifiers / sizeof *notifiers; // Populate array of notifiers. @@ -2625,7 +2623,7 @@ static void test_notifiers_with_strategy(universal_notifier_t::notifier_strategy if (!poll_notifier(notifiers[i])) { err(L"Universal variable notifier (%lu) %p polled failed to notice changes, with " L"strategy %d", - i, notifiers[i], (int)strategy); + i, notifiers[i].get(), (int)strategy); } } @@ -2648,11 +2646,6 @@ static void test_notifiers_with_strategy(universal_notifier_t::notifier_strategy (int)strategy); } } - - // Clean up. - for (size_t i = 0; i < notifier_count; i++) { - delete notifiers[i]; - } } static void test_universal_notifiers() {