Welcome in the forum, Stefan!
I have got a bunch of projects using malloc() and free(), no errors so far. There is a dialog option in .cydwr view, System tab to set the amount of space to put aside for the heap. Are you using fprintf() converting floats? This is the only point (under certain conditions only) where I found something going amiss.
Usual errors (as you certainly know) is changing memory locations outside of the requested area or freeing twice the same block apart from running out of ram and not checking for a NULL pointer. I would suggest to concentrate on those.
PS: Where in Germany are you located? I live near Bremen.
Hi Bob, thanks for your swift answer on this. I currently live in the far south of germany, close to Basel (Weil am Rhein). What brought you to Bremen ?:-)
I found a thread on the dynamic memory allocation subject where you referenced the mandatory use of init_mempool. Is this obsolete ? As said, I cannot even find the function on my DevEnv. The error is in fact strange. I am aware of the heap size settings and have reserved sufficient space there. Problem is, the calloc function returns a pointer fine but factually overwrites another allocated space in memory. See the code snippet below. I use a circular buffer to get something like an event driven processing to manage several UARTs in parallel. The evt pointer holds the reference to a given event popped from this queue. It gets completely messed up after the calloc further down.
I was hoping to get a clue on the init_mempool function which makes sense - yet, I cannot find it anywhere...
Thanks for your help and beste Grüsse
// if there are stored events and the last event has been processed
if(!empty(&evtBuffer) && (evt == NULL))
/* Read next Event*/
evt = (struct Event *)popqueue(&evtBuffer);
if(evt != NULL)
char cursor = "> ";
// nach ein paar commandos gehen nach diesem calloc die evt daten verloren !!!!
usb_response = calloc(evt->DataSize + sizeof(cursor)+ EVENT_SOURCE_SIZE + 1,sizeof(char) );
// Create Error Event here: out of memory
// Handle all events that come from Readers
if(evt->Type == EVENT_TYPE_UART_WORD_RECEIVED)
Stefan, please do not provide code snippets only. Can you please post your complete project, so that we all can have a look at all of your settings? To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.
Yes, quite some code. I had made a similar bug recently (addressing malloced memory outside its range). I took the following fixup to find my bug:
Encapsulate calloc() and free() as
void * Stefan_calloc(size_t Number, size_t ElementSize)
In this function you can store every pointer allocated and compare if a freed pointer is within that list, so you can see when the system starts going bad. One of the preceding calls will have gone amiss. You could centralize the out-of-memory check, no need to check after each calloc(). Additionally you could request for some more memory, initialize it with some known pattern and check at free() whether it is still untouched.
I get your point. I do use sprintf at some point but what puzzles me is that it is exactly that single calloc that overwrites memory.
I will have to check for double free()'s, after many hours of coding java and c# I got lazy with memory management discipline.
Back to the init_mempool question - is this call not needed anymore ?
Afaik init_mempool is still needed for PSoC3 Keil C-compiler. PSoC5 gcc package does not need this.function. Normal sprintf() is not reentrant and converting floats has problems with malloc.
Problem is solved, init_mempool is not needed. I had a logical bug in the FlashPop function. As it is, I removed all dynamic memory handling as well as the ring buffer to lower complexity of my code. Anyone interested in working versions of the many functions in my project - give me a shout.