# vApplicationMallocFailedHook()  function redefinition problem

**URL:** <https://forums.freertos.org/t/vapplicationmallocfailedhook-function-redefinition-problem/7529>\
**Category:** Kernel\
**Created:** [November 19, 2018, 10:10am UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-function-redefinition-problem/7529 "2018-11-19T10:10:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 19, 2018, 10:10am UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-function-redefinition-problem/7529/1 "2018-11-19T10:10:21Z")

</div>

**[claudiorossi](https://sourceforge.net/u/claudiorossi/)** wrote on [Monday, November 19, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/368ac619ec/#af6e):

Hi

I’am developing a FreeRTOS application on Zynq7000 with SDK 2018.2 and I have problem with vApplicationMallocFailedHook function.

I have defined my own function but the standard function present in portZynq7000.c is always called.  
I have defined my own vApplicationAssert function too, but in this case the redefinition works fine.  
The two standard functions are defined in the same portZynq7000.c file so I don’t understand the  
different behaviour.

In source file I get the following comment:

/\* This default malloc failed hook does nothing and is declared as a weak symbol  
to allow the application writer to override this default by providing their own  
implementation in the application code. \*/  
void vApplicationMallocFailedHook( void )  
{  
xil\_printf( “vApplicationMallocFailedHook() called\n” );  
}

Someone have suggestions? Do I need to set some different?

Thanks for help

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 19, 2018, 11:28am UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-function-redefinition-problem/7529/2 "2018-11-19T11:28:56Z")

</div>

**[richarddamon](https://sourceforge.net/u/richarddamon/)** wrote on [Monday, November 19, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/368ac619ec/#ddaa):

If it is defined as a weak symbol as the comment says (but I don’t see the directive that makes it a weak definition, maybe you trimmed that directive off) then if you define you version as non-weak it will over-ride the weak definition. If you copied the weak definition directive into your code, then deending on the order the files are processed will control which definition gets used.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 19, 2018, 1:30pm UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-function-redefinition-problem/7529/3 "2018-11-19T13:30:36Z")

</div>

**[claudiorossi](https://sourceforge.net/u/claudiorossi/)** wrote on [Monday, November 19, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/368ac619ec/#40ea):

Hi Richard

The weak attribute is defined at top of PortZynq7000.c file:

void vApplicationAssert( const char \*pcFileName, uint32\_t ulLine ) **attribute** ((weak));  
void vApplicationTickHook( void ) **attribute** ((weak));  
void vApplicationIdleHook( void ) **attribute** ((weak));  
void vApplicationMallocFailedHook( void ) **attribute** ((weak));  
void vApplicationStackOverflowHook( TaskHandle\_t xTask, char \*pcTaskName ) **attribute** ((weak));

I only rewrite the function without add any directives.  
I tried to redefine the vApplicationIdleHook function too, but the standard function is always called.

In Xilinx SDK the FreeRTOS is compiled as a standalone library. May be this the problem?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 19, 2018, 2:27pm UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-function-redefinition-problem/7529/4 "2018-11-19T14:27:32Z")

</div>

**[claudiorossi](https://sourceforge.net/u/claudiorossi/)** wrote on [Monday, November 19, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/368ac619ec/#3593):

I have solved the problem.  
I need to insert the functions prototype (without weak attribute) in FreeRtosConfig.h file.  
Unfortunately this file is automatically generated by Xilinx SDK tool so I need to modify  
the tcl script file used by the tool.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [November 19, 2018, 9:51pm UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-function-redefinition-problem/7529/5 "2018-11-19T21:51:05Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Monday, November 19, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/368ac619ec/#1611):

Is your code using C++? It might be that you need the extern “C” part of the decleration, that might get automatically added in the FreeRTOSConfig.h file

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [November 19, 2018, 9:51pm UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-function-redefinition-problem/7529/6 "2018-11-19T21:51:09Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Monday, November 19, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/368ac619ec/#b379):

Is your code using C++? It might be that you need the extern “C” part of the decleration, that might get automatically added in the FreeRTOSConfig.h file

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 28, 2018, 8:44am UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-function-redefinition-problem/7529/7 "2018-11-28T08:44:50Z")

</div>

**[claudiorossi](https://sourceforge.net/u/claudiorossi/)** wrote on [Wednesday, November 28, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/368ac619ec/#1684):

Yes, is a C++ code, but compilation work even if i do not add extern “C” to FreeRTOSConfig.h file.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 28, 2018, 11:09am UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-function-redefinition-problem/7529/8 "2018-11-28T11:09:00Z")

</div>

**[richarddamon](https://sourceforge.net/u/richarddamon/)** wrote on [Wednesday, November 28, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/368ac619ec/#c974):

The symptoms sound like the need to mark the file as extern “C”. It may be that by putting the decleration in FreeRTOSConfig.h it is getting wrapped in the extern “C” automatically. It could be that the tool’s autogenerated header wraps most of FreeRTOSConfig.h in an extern “C” section, which could be it being helpful as you often end up declaring function there for use as call backs for FreeRTOS.

The solution to the regeneration is to not add the decleration to FreeRTOSConfig.h, but to add the extern “C” lines in the file where you define the function, either wrapping a pre-decleration or the definition.

I often have a ‘hooks’ file for the various FreeRTOS hooks that has near the beginning

extern “C” {

then has the various hook functions for FreeRTOS

and then the section ends with  
}

Note, the functions in between are still C++ functions, but use the C linkage rules so they internally use the C linkage nameing rules, not the C++ rules that support overloading, so FreeRTOS can call them.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 30, 2018, 8:46am UTC](https://forums.freertos.org/t/vapplicationmallocfailedhook-function-redefinition-problem/7529/9 "2018-11-30T08:46:57Z")

</div>

**[claudiorossi](https://sourceforge.net/u/claudiorossi/)** wrote on [Friday, November 30, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/368ac619ec/#eb1e):

Hi Richard

Your solution is correct. Adding extern “C” {…declaration…} to the file where I have my functions solve the issue.

Thank you
