Running out of heap / malloc/free usage within FreeRTOS

pugglewuggle wrote on Monday, 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:

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

		Initialize response buffer to 0/null
		// Then fill this with good stuff :-)
	return ret;

rtel wrote on Tuesday, 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.

pugglewuggle wrote on Friday, February 26, 2016:

You’re correct, I found the problem. It was indeed an errant malloc :slight_smile: 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.

edwards3 wrote on Saturday, February 27, 2016:

its the same as the C library assert.h assert(). Did you see

pugglewuggle wrote on Wednesday, March 02, 2016:

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