diff --git a/features/frameworks/greentea-client/source/greentea_metrics.cpp b/features/frameworks/greentea-client/source/greentea_metrics.cpp index a196cfbc3a..35cfb271e8 100644 --- a/features/frameworks/greentea-client/source/greentea_metrics.cpp +++ b/features/frameworks/greentea-client/source/greentea_metrics.cpp @@ -107,7 +107,11 @@ MBED_UNUSED static void send_stack_info() // Print info for all other threads uint32_t thread_n = osThreadGetCount(); - osThreadId_t *threads = new osThreadId_t[thread_n]; + osThreadId_t *threads = new (std::nothrow) osThreadId_t[thread_n]; + // Don't fail on lack of memory + if (!threads) { + goto end; + } thread_n = osThreadEnumerate(threads, thread_n); for(size_t i = 0; i < thread_n; i++) { @@ -117,6 +121,7 @@ MBED_UNUSED static void send_stack_info() delete[] threads; +end: mutex->unlock(); } diff --git a/platform/mbed_stats.c b/platform/mbed_stats.c index 11d870b7d1..81b1a6a3ce 100644 --- a/platform/mbed_stats.c +++ b/platform/mbed_stats.c @@ -41,7 +41,10 @@ void mbed_stats_stack_get(mbed_stats_stack_t *stats) osThreadId_t *threads; threads = malloc(sizeof(osThreadId_t) * thread_n); - MBED_ASSERT(threads != NULL); + // Don't fail on lack of memory + if (!threads) { + return; + } osKernelLock(); thread_n = osThreadEnumerate(threads, thread_n); @@ -69,7 +72,10 @@ size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count) osThreadId_t *threads; threads = malloc(sizeof(osThreadId_t) * count); - MBED_ASSERT(threads != NULL); + // Don't fail on lack of memory + if (!threads) { + return 0; + } osKernelLock(); count = osThreadEnumerate(threads, count);