Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

Running out of heap / malloc/free usage within FreeRTOS

Posted by pugglewuggle on February 22, 2016

I've got a function that returns a pointer that is malloc'd within that function, and then free that memory outside that function from within the calling function like so:

It would appear that malloc is working but it is never freeing. Eventually, after enough iterations my MallocFailed hook kicks in. I am using heap_4.c

This execution is happening within the same task. Please help!

~~~~

void doSomething(){ char *str = NULL; //Local string buffer str = mallocInside(4);

if (str != NULL)
{
    // Do lots of stuff, then:
    vPortFree(str);
}

}

char* mallocInside(int x) { char* ret = (char*) pvPortMalloc(sizeof(char) * 255); if(ret != NULL){

	Initialize response buffer to 0/null
	memset(ret,0,sizeof(ret));

	// Then fill this with good stuff :-)
}
return ret;

} ~~~~


Running out of heap / malloc/free usage within FreeRTOS

Posted by rtel on February 23, 2016

We have run much more stringent tests than those shown in your code with no issues, so I'm guessing something in the "do lots of stuff, then" block is clobbering the memory somehow so it doens't get freed correct.y.

Do you have configASSERT() defined?

Also, the memset() call is using sizeof(ret), which is sizeof( char* ), which is 1. I don't think that is what you intended to do.


Running out of heap / malloc/free usage within FreeRTOS

Posted by pugglewuggle on February 26, 2016

You're correct, I found the problem. It was indeed an errant malloc :-) Also, the ret issue was due to some copied C++ std::string code, so my bad on the example... I just hadn't fixed it yet at some point. Do you have an article as to how to effectively user configASSERT() and describe what it does/how to use it in practice? I haven't really seen a good description of this so far.


Running out of heap / malloc/free usage within FreeRTOS

Posted by edwards3 on February 27, 2016

its the same as the C library assert.h assert(). Did you see http://www.freertos.org/a00110.html#configASSERT?


Running out of heap / malloc/free usage within FreeRTOS

Posted by pugglewuggle on March 2, 2016

I did, I just haven't really ever used it.


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists