When stack stats enabled, prevent exceptions if memory allocations fail

pull/7730/head
David Saada 2018-08-08 16:18:29 +03:00
parent 273a052e89
commit cfe7df28c9
2 changed files with 14 additions and 3 deletions

View File

@ -107,7 +107,11 @@ MBED_UNUSED static void send_stack_info()
// Print info for all other threads // Print info for all other threads
uint32_t thread_n = osThreadGetCount(); 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); thread_n = osThreadEnumerate(threads, thread_n);
for(size_t i = 0; i < thread_n; i++) { for(size_t i = 0; i < thread_n; i++) {
@ -117,6 +121,7 @@ MBED_UNUSED static void send_stack_info()
delete[] threads; delete[] threads;
end:
mutex->unlock(); mutex->unlock();
} }

View File

@ -41,7 +41,10 @@ void mbed_stats_stack_get(mbed_stats_stack_t *stats)
osThreadId_t *threads; osThreadId_t *threads;
threads = malloc(sizeof(osThreadId_t) * thread_n); threads = malloc(sizeof(osThreadId_t) * thread_n);
MBED_ASSERT(threads != NULL); // Don't fail on lack of memory
if (!threads) {
return;
}
osKernelLock(); osKernelLock();
thread_n = osThreadEnumerate(threads, thread_n); 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; osThreadId_t *threads;
threads = malloc(sizeof(osThreadId_t) * count); threads = malloc(sizeof(osThreadId_t) * count);
MBED_ASSERT(threads != NULL); // Don't fail on lack of memory
if (!threads) {
return 0;
}
osKernelLock(); osKernelLock();
count = osThreadEnumerate(threads, count); count = osThreadEnumerate(threads, count);