cancel
Showing results for 
Search instead for 
Did you mean: 

pitch = assertion "REENT malloc succeeded" failed: file "/build/gnu-tools-for-stm32_10.3-2021.10.20211105-1100/src/newlib/newlib/libc/stdlib/dtoa.c", line 234

Angle
Associate II

what mean by this error? don't understand

4 REPLIES 4
FBL
ST Employee

Hi @Angle​ 

A similar issue has been reported in this post . It might help you. If not, could you please provide more details about your use case.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.


I'm out of offce with limited access to my emails.
Happy New Year!
Zeron
Associate III

Hello, I'm working on an ARM embedded programming course and I'm having exactly the same problem.

I'm using the Nucleo F411RE board.

Here is my code:

1. main.c

 

 

 

#include <stdio.h>
#include "stm32f4xx.h"
#include "signals.h"
#include "usart.h"
#include "arm_math.h"

#ifndef STM32F411xE
#define STM32F411xE
#endif

extern float _5hz_signal[HZ_5_SIG_LEN];
extern float32_t input_signal_f32_1kHz_15kHz[KHZ1_15_SIG_LEN];
extern float32_t impulse_response[IMP_RSP_LENGTH];

//float32_t output_signal_arr[KHZ1_15_SIG_LEN + IMP_RSP_LENGTH -1];

float32_t ReX[HZ_5_SIG_LEN/2];		// Frequency domain cosine wave
float32_t ImX[HZ_5_SIG_LEN/2];		// Frequency domain sine wave

void calc_sig_dft(float32_t *, float32_t *, float32_t *, uint32_t);
void serial_plot_rex();
void dft_get_output_mag();

void pseudo_dly(int);
void fpu_enable();

int main()
{
	/* Enable Floating Point Unit*/
	fpu_enable();

	// Initialize the USART
	usart2_tx_init();

	calc_sig_dft((float32_t *) input_signal_f32_1kHz_15kHz,
	 		     (float32_t *) ReX,
 			     (float32_t *) ImX,
			     (uint32_t) KHZ1_15_SIG_LEN);

	//dft_get_output_mag();

	serial_plot_rex();



	while(1)
	{
	}
}

void dft_get_output_mag()
{
	for( int k = 0; k < KHZ1_15_SIG_LEN / 2; k++)
	{
		ReX[k] = fabs(ReX[k]);
	}
}

void calc_sig_dft(float32_t *sig_src_arr,
				   float32_t *sig_dest_rex_arr,
				   float32_t *sig_dest_imx_arr,
				   uint32_t sig_length)
{
	int i, k;

	//Compute DFT
	for( k = 0; k < sig_length / 2; k++ )
	{
		for ( i = 0; i < sig_length; i++)
		{
			sig_dest_rex_arr[k] = sig_src_arr[i] * cos( 2 * PI * k * i / sig_length);
			sig_dest_imx_arr[k] = sig_src_arr[i] * sin( 2 * PI * k * i / sig_length);
		}
	}
}

void serial_plot_rex()
{
	for(int j = 0; j < KHZ1_15_SIG_LEN/2; j++)
	{
		printf("%f,\r\n", ReX[j]);
		pseudo_dly(9000);
	}
}

void pseudo_dly( int dly )
{
	for ( int i = 0 ; i < dly; i++ ){}
}

void fpu_enable()
{
	/* Enable CP10 and CP11 full access (CP = CoProcessor)*/

	SCB->CPACR |= ( 0x3 << 2 * 10 );
	SCB->CPACR |= ( 0x3 << 2 * 11 );

}

 

 

 

 

 2. signals.h

 

 

 

/*
 * signals.h
 *
 *  Created on: Jun 27, 2024
 *      Author: Administrator
 */

#include <stdint.h>
#include "arm_math.h"

#ifndef SIGNALS_H_
#define SIGNALS_H_

#define HZ_5_SIG_LEN		301
#define KHZ1_15_SIG_LEN		320
#define IMP_RSP_LENGTH		29


#endif /* SIGNALS_H_ */

 

 

 

 

3. usart.h

 

 

 

/*
 * usart.h
 *
 *  Created on: Jul 12, 2024
 *      Author: Administrator
 */

#ifndef USART_H_
#define USART_H_

#include <stdint.h>
#include "stm32f4xx.h"

#define SYSFREQ			16000000
#define APB1CLK			SYSFREQ
#define USART_BAUDRATE	115200
//#define USART_BAUDRATE	9600

void usart2_tx_init();
static void usart2_set_baudrate( uint32_t, uint32_t);
static uint16_t compute_usart2_bd( uint32_t, uint32_t);
void usart2_write(int);

#endif /* USART_H_ */

 

 

 

 

3. signals.c

 

 

 

/*
 * signals.c
 *
 *  Created on: Jun 27, 2024
 *      Author: Administrator
 */

#ifndef __SIGNALS_H__
#define __SIGNALS_H__

#include "signals.h";

/* Kernel lowpass filter with cutoff frequency of 6 kHz */
const float32_t  impulse_response[IMP_RSP_LENGTH] = {
  -0.0018225230f, -0.0015879294f, +0.0000000000f, +0.0036977508f, +0.0080754303f, +0.0085302217f, -0.0000000000f, -0.0173976984f,
  -0.0341458607f, -0.0333591565f, +0.0000000000f, +0.0676308395f, +0.1522061835f, +0.2229246956f, +0.2504960933f, +0.2229246956f,
  +0.1522061835f, +0.0676308395f, +0.0000000000f, -0.0333591565f, -0.0341458607f, -0.0173976984f, -0.0000000000f, +0.0085302217f,
  +0.0080754303f, +0.0036977508f, +0.0000000000f, -0.0015879294f, -0.0018225230f
};

float _5hz_signal[HZ_5_SIG_LEN]=
{
	0,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,1.2246e-16,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,-2.4493e-16,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,3.6739e-16,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,-4.8986e-16,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,6.1232e-16,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,-7.3479e-16,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,-2.6955e-15,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,-9.7972e-16,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,1.1022e-15,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,-1.2246e-15,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,-2.2056e-15,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,-1.4696e-15,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,-1.9607e-15,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,5.3909e-15,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,5.3897e-15,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,-9.0649e-15,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,5.6346e-15,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,-2.2044e-15,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,5.8795e-15,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,-2.4493e-15,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,-9.8096e-16,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,4.4112e-15,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,6.3694e-15,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,-2.9392e-15,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,-4.911e-16,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,3.9213e-15,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,-7.3516e-15,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,-3.429e-15,0.30902,0.58779,0.80902,0.95106,1,0.95106,0.80902,0.58779,0.30902,-1.238e-18,-0.30902,-0.58779,-0.80902,-0.95106,-1,-0.95106,-0.80902,-0.58779,-0.30902,-1.0779e-14

};

float32_t input_signal_f32_1kHz_15kHz[KHZ1_15_SIG_LEN] =
{
+0.0000000000f, +0.5924659585f, -0.0947343455f, +0.1913417162f, +1.0000000000f, +0.4174197128f, +0.3535533906f, +1.2552931065f,
+0.8660254038f, +0.4619397663f, +1.3194792169f, +1.1827865776f, +0.5000000000f, +1.1827865776f, +1.3194792169f, +0.4619397663f,
+0.8660254038f, +1.2552931065f, +0.3535533906f, +0.4174197128f, +1.0000000000f, +0.1913417162f, -0.0947343455f, +0.5924659585f,
-0.0000000000f, -0.5924659585f, +0.0947343455f, -0.1913417162f, -1.0000000000f, -0.4174197128f, -0.3535533906f, -1.2552931065f,
-0.8660254038f, -0.4619397663f, -1.3194792169f, -1.1827865776f, -0.5000000000f, -1.1827865776f, -1.3194792169f, -0.4619397663f,
-0.8660254038f, -1.2552931065f, -0.3535533906f, -0.4174197128f, -1.0000000000f, -0.1913417162f, +0.0947343455f, -0.5924659585f,
+0.0000000000f, +0.5924659585f, -0.0947343455f, +0.1913417162f, +1.0000000000f, +0.4174197128f, +0.3535533906f, +1.2552931065f,
+0.8660254038f, +0.4619397663f, +1.3194792169f, +1.1827865776f, +0.5000000000f, +1.1827865776f, +1.3194792169f, +0.4619397663f,
+0.8660254038f, +1.2552931065f, +0.3535533906f, +0.4174197128f, +1.0000000000f, +0.1913417162f, -0.0947343455f, +0.5924659585f,
+0.0000000000f, -0.5924659585f, +0.0947343455f, -0.1913417162f, -1.0000000000f, -0.4174197128f, -0.3535533906f, -1.2552931065f,
-0.8660254038f, -0.4619397663f, -1.3194792169f, -1.1827865776f, -0.5000000000f, -1.1827865776f, -1.3194792169f, -0.4619397663f,
-0.8660254038f, -1.2552931065f, -0.3535533906f, -0.4174197128f, -1.0000000000f, -0.1913417162f, +0.0947343455f, -0.5924659585f,
+0.0000000000f, +0.5924659585f, -0.0947343455f, +0.1913417162f, +1.0000000000f, +0.4174197128f, +0.3535533906f, +1.2552931065f,
+0.8660254038f, +0.4619397663f, +1.3194792169f, +1.1827865776f, +0.5000000000f, +1.1827865776f, +1.3194792169f, +0.4619397663f,
+0.8660254038f, +1.2552931065f, +0.3535533906f, +0.4174197128f, +1.0000000000f, +0.1913417162f, -0.0947343455f, +0.5924659585f,
+0.0000000000f, -0.5924659585f, +0.0947343455f, -0.1913417162f, -1.0000000000f, -0.4174197128f, -0.3535533906f, -1.2552931065f,
-0.8660254038f, -0.4619397663f, -1.3194792169f, -1.1827865776f, -0.5000000000f, -1.1827865776f, -1.3194792169f, -0.4619397663f,
-0.8660254038f, -1.2552931065f, -0.3535533906f, -0.4174197128f, -1.0000000000f, -0.1913417162f, +0.0947343455f, -0.5924659585f,
-0.0000000000f, +0.5924659585f, -0.0947343455f, +0.1913417162f, +1.0000000000f, +0.4174197128f, +0.3535533906f, +1.2552931065f,
+0.8660254038f, +0.4619397663f, +1.3194792169f, +1.1827865776f, +0.5000000000f, +1.1827865776f, +1.3194792169f, +0.4619397663f,
+0.8660254038f, +1.2552931065f, +0.3535533906f, +0.4174197128f, +1.0000000000f, +0.1913417162f, -0.0947343455f, +0.5924659585f,
-0.0000000000f, -0.5924659585f, +0.0947343455f, -0.1913417162f, -1.0000000000f, -0.4174197128f, -0.3535533906f, -1.2552931065f,
-0.8660254038f, -0.4619397663f, -1.3194792169f, -1.1827865776f, -0.5000000000f, -1.1827865776f, -1.3194792169f, -0.4619397663f,
-0.8660254038f, -1.2552931065f, -0.3535533906f, -0.4174197128f, -1.0000000000f, -0.1913417162f, +0.0947343455f, -0.5924659585f,
+0.0000000000f, +0.5924659585f, -0.0947343455f, +0.1913417162f, +1.0000000000f, +0.4174197128f, +0.3535533906f, +1.2552931065f,
+0.8660254038f, +0.4619397663f, +1.3194792169f, +1.1827865776f, +0.5000000000f, +1.1827865776f, +1.3194792169f, +0.4619397663f,
+0.8660254038f, +1.2552931065f, +0.3535533906f, +0.4174197128f, +1.0000000000f, +0.1913417162f, -0.0947343455f, +0.5924659585f,
+0.0000000000f, -0.5924659585f, +0.0947343455f, -0.1913417162f, -1.0000000000f, -0.4174197128f, -0.3535533906f, -1.2552931065f,
-0.8660254038f, -0.4619397663f, -1.3194792169f, -1.1827865776f, -0.5000000000f, -1.1827865776f, -1.3194792169f, -0.4619397663f,
-0.8660254038f, -1.2552931065f, -0.3535533906f, -0.4174197128f, -1.0000000000f, -0.1913417162f, +0.0947343455f, -0.5924659585f,
-0.0000000000f, +0.5924659585f, -0.0947343455f, +0.1913417162f, +1.0000000000f, +0.4174197128f, +0.3535533906f, +1.2552931065f,
+0.8660254038f, +0.4619397663f, +1.3194792169f, +1.1827865776f, +0.5000000000f, +1.1827865776f, +1.3194792169f, +0.4619397663f,
+0.8660254038f, +1.2552931065f, +0.3535533906f, +0.4174197128f, +1.0000000000f, +0.1913417162f, -0.0947343455f, +0.5924659585f,
+0.0000000000f, -0.5924659585f, +0.0947343455f, -0.1913417162f, -1.0000000000f, -0.4174197128f, -0.3535533906f, -1.2552931065f,
-0.8660254038f, -0.4619397663f, -1.3194792169f, -1.1827865776f, -0.5000000000f, -1.1827865776f, -1.3194792169f, -0.4619397663f,
-0.8660254038f, -1.2552931065f, -0.3535533906f, -0.4174197128f, -1.0000000000f, -0.1913417162f, +0.0947343455f, -0.5924659585f,
-0.0000000000f, +0.5924659585f, -0.0947343455f, +0.1913417162f, +1.0000000000f, +0.4174197128f, +0.3535533906f, +1.2552931065f,
+0.8660254038f, +0.4619397663f, +1.3194792169f, +1.1827865776f, +0.5000000000f, +1.1827865776f, +1.3194792169f, +0.4619397663f,
+0.8660254038f, +1.2552931065f, +0.3535533906f, +0.4174197128f, +1.0000000000f, +0.1913417162f, -0.0947343455f, +0.5924659585f,
+0.0000000000f, -0.5924659585f, +0.0947343455f, -0.1913417162f, -1.0000000000f, -0.4174197128f, -0.3535533906f, -1.2552931065f,
};

#endif

 

 

 

 

4. usart.c

 

 

 

/*
 * usart.c
 *
 *  Created on: Jul 12, 2024
 *      Author: Administrator
 */
#include "usart.h"

int __io_putchar(int ch)
{
	usart2_write(ch);

	return ch;
}

void usart2_tx_init()
{
	/***** Configure UART GPIO Pin *****/

	// Enable clock access for GPIOA
	RCC->AHB1ENR |=		( 0x1 << 0 );			// GPIOAEN

	// Set PA2 mode to alternate function mode
	GPIOA->MODER |= 	( 0x2 << 2 * 2 );

	// Set PA2 alternate function mode to USART2_TX(AF07)
	GPIOA->AFR[0] = 	0x0;					// Clear all bits
	GPIOA->AFR[0] |=	( 0x7 << 2 * 4 );		// Enable first three bits of 4th bit block
	GPIOA->AFR[0] &=~	( 0x8 << 2 * 4 );		// Disable last bit of 4th bit block


	/***** Configure USART *****/

	// Enable clock access to USART2
	RCC->APB1ENR |=		( 0x1 << 17 );			// USART2EN

	// configure baudrate
	// usart2_set_baudrate( APB1CLK, USART_BAUDRATE );
	USART2->BRR |= compute_usart2_bd(APB1CLK, USART_BAUDRATE);

	// Enable USART2 Tx
	USART2->CR1 |=		( 0x1 << 3 );			// TE	( Tx Enable )

	// Set USART to Half-Duplex Single Wire mode
	USART2->CR3 |=		( 0x1 << 3 );			// HDSEL

	// Enable USART module
	USART2->CR1 |=		( 0x1 << 13 );			// UE	( USART Enable )
}

void usart2_write(int ch)
{
	/* Make sure transmit data register is emtpy */
	while( ! ( USART2->SR & ( 0x1 << 7 ) ) ) {}

	/* write to transmit data register */
	USART2->DR = ( ch & 0xFF );
}

static void usart2_set_baudrate( uint32_t periph_clk, uint32_t baudrate)
{
	USART2->BRR = compute_usart2_bd( periph_clk, baudrate );
}

static uint16_t compute_usart2_bd( uint32_t periph_clk, uint32_t baudrate)
{
	return (( periph_clk + ( baudrate / 2 )) / baudrate );
}

 

 

 

 

Things things went wrong after the call to printf. This printf call did work in another earlier project where there was not yet such a resource consuming calc_sig_dft() function.

I've encountered several unexplainable issues while working on this problem. Let me explain what I have found out this far.

If I debug the program, it works fine all the way until printf is being called. After 'step into' printf("%f\r\n", ReX[j]); the program hangs on an infinite while(1){} loop. When I click suspend I see the program being stuck in _exit() from syscalls.c.

In the thread call stack I saw that _vsprintf_r()  and _dtoa() from 'stdio' got called. One of the arguments of these functions is a struct _reent*.

I decided to go and take a look at this struct's definition, it resides in 'sys/reent.h'

In this header file I also discovered the actual assertion that failed and caused the hang. But from here on things started getting realy weird from my point of view.

There's this portion of code in 'reent.h' :

 

 

 

 

 

#ifdef _REENT_CHECK_VERIFY
#include <assert.h>
#define __reent_assert(x) ((x) ? (void)0 : __assert_func(__FILE__, __LINE__, (char *)0, "REENT malloc succeded"))
#else
#define __reent_assert(x) ((void)0)
#endif

 

 

 

 

 

 

I find myself unable to find out what __assert_func() realy is or does. I do know that the string "REENT malloc succeded" is the actual string that shows up in the output of my program and gets displayed in my terminal emulater.

So, I decided to just find out where exactly _REENT_CHECK_VERIFY was defined. It turned out to be in 'newlib.h'.

I removed the definition, rebuilt and debugged the program once more. No difference, still the same error.

Then I decided to just comment out the line

#define __reent_assert(x) ((x) ? (void)0 : __assert_func..........

and replace it with

#define __reent_assert(x) ((void)0)

I expected this might work to cancel out the use of the assertion. But.. it didn't. This made me wonder if the compiler is using precompiled headers for building my project's .elf binary. The changes I made in the header files just didn't seem to take effect in the compiled output. And my program keeps getting stuck on the same assertion over and over again.

If anyone could help me find out more about __assert_func() I would be happy. I would also like to know wether or not the header files do have some sort of precompiled built versions of them that are used by CubeMX because making changes to the header files just does not seem to have any kind of effect.

I can only suppose this failure might have something to do with the some kind of resources of the chip being taken by the calc_sig_dft() function or one of its underlying calls.

 

Well anyways, in the meanwhile I just tried something new that I didn't try earlier on and that was placing a printf("%f,\r\n", 14.432f); after the call to usart2_tx_init(); and before calc_sig_dft(...); just to see if this works. And now all of the sudden it works. If a say printf("%f,\r\n",0.0f) the results are the same as before once again. Now I use printf("%f,\r\n",0.00001f) and yet again the program does seem to work again.

I don't know what goes wrong but I would like to know. And if anyone could give me any explaination why commenting out those definitions in the header files didn't give any effect I would also realy like to know because I just don't like it.

I hope someone is able to replay this bug and find out what is going wrong.

I'm using the header files from the latest 'STM32Cube MCU Package for STM32F4 series' without the patch. I also use 'libarm_cortexM4lf_math.a' from this package.

I'm also using 'arm_cortexM4b_math.lib', 'arm_cortexM4bf_math.lib', 'arm_cortexM4l_math.lib' and 'arm_cortexM4'f_math.lib' from another source, namely:

https://github.com/pavel-a/stm32f4_libs/tree/master/STM32Cube_FW_F4/Drivers/CMSIS/Lib 

I don't know how to build these libs using the files out of the MCU Package. The version numbers differ between 1.24 and 1.28.

I also noticed that _dtoa() is declared in 'stdlib.h' but I can't find a definition of this function.

This again reaffirms my impression of precompiled headers or libraries being used, just like the fact the changes I made in the header files didn't take any effect. I would realy like to know a bit more about this.

That was about it. I hope someone will find out where things go wrong without the obsolete call to printf before the calc_sig_dft(...); and why the program does work with it, just because I don't like having it there and it's an ugly workaround that kind of works but still doesn't fix the real issue.

Thank you.

Zeron
Associate III

I just found out that I used HZ_5_SIG_LEN in the declaration of ReX[], where I should have used KHZ_15_SIG_LEN.

Replacing this did make the program work without the obsolete printf("%f\r\n", 0.00001f) call. Nevertheless it still doesn't make sense to me why removing the #define _REENT_CHECK_VERIFY from 'newlib.h' and the assertion from reent.h did not take effect. I also still don't understand why it didn't work without the obsolete printf() but did with it.

Zeron
Associate III

My last reply to my previous reply, to finish this post, it must been due to the use of the precompiled libraries, since there aren't any .c source files provided, only .h header files.