Skip to main content
nrose
Associate II
April 10, 2019
Question

I'm running C++ code using the HAL on a STM32F030x8. If I perform a "new" and there is not enough heap left, rather than returning 0, it sits on a BKPT 0xAB in _ttywrch and hangs. This is not ideal. Any ideas ?

  • April 10, 2019
  • 6 replies
  • 1519 views

..

This topic has been closed for replies.

6 replies

S.Ma
Principal
April 10, 2019

Speaking only to what I know, malloc/new/free dynamic heap allocation is to be avoided in embedded space due to the SRAM that always need to be optimized. "unions" with static memory allocation or temporary storate in the stack would be prefferable when possible.

Tesla DeLorean
Guru
April 10, 2019

In Keil perhaps implement the hosting on your target properly so it outputs whatever message it is trying to output to the terminal.

There aren't infinite resources in embedded, perhaps there is a throw/catch mechanism to handle this, or some kind of structure exception handler.

//****************************************************************************
// Hosting of stdio functionality through USART3
//****************************************************************************
 
/* Implementation of putchar (also used by printf function to output data) */
int SendChar(int ch) /* Write character to Serial Port */
{
 while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); // Wait for Empty
 USART_SendData(USART3, ch);
 
 return(ch);
}
 
//****************************************************************************
 
#include <rt_misc.h>
 
#pragma import(__use_no_semihosting_swi)
 
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
 
int fputc(int ch, FILE *f) { return (SendChar(ch)); }
 
int ferror(FILE *f)
{
 /* Your implementation of ferror */
 return EOF;
}
 
void _ttywrch(int ch) { SendChar(ch); }
 
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
 
//****************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
nrose
nroseAuthor
Associate II
April 10, 2019

You are right - it is trying to write an error message to an unimplemented character routine. I put in my own _ttywrch and I see the message "SIGABRT: Abnormal termination".

Presumably there is some kind of exception handler being triggered - any idea how I can defeat it ? I'll look at the rt_misc error handling functions.

Bob S
Super User
April 11, 2019

Unlike malloc(), new is never supposed to return zero/NULL/nullptr. It either returns the requested item, fully constructed or it throws an exception. UNLESS you use the "no throw" syntax for new :)

http://www.cplusplus.com/reference/new/operator%20new/