This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

why am i running out of heap memory when i am freeing my linked list?

i am creating a user interface using the sh1106 driver for a 128x64 pixel oled screen. so every screen is a list of items to be scrolled; however almost every screen i am using a linked list for the list of elements. in one screen i am creating a linked list and freeing it for about 12 to 15 times but when i reach a number like that the malloc is returning NULL so that means the malloc is not able to find the memory space i need. but what could be the cause? could it be a memory leak from the way i am freeing the linked list? could it be that the memory free space has become too fragmented? if so why only this linked list is messing up?

thanks in advance for ur help guys! and if there is anything needs clearing up or u need some extra info let me now and ill post it.

the code is below: (please keep in mind that i am extracting information from database saved in flash)

void createNotesListFromDb(linkedListNotes_t **head){

notes_t notesStructure;
uint8_t tuningID = dbGetTuningID(instrumentRefID);
tuningRefID = tuningID;
tuningNotesCount = dbGetTuningNotesCount(tuningID);
nbrOfNoteNodes = 0;
char notesTransferArray[4];


for(uint8_t nmbr = 1;nmbr <= tuningNotesCount; nmbr++)
{
	notesStructure = dbGetNote(tuningID, nmbr);
	
	// the next part is to fill a new node with desired values and information
	linkedListNotes_t * newNoteNode;
	newNoteNode = (linkedListNotes_t*)malloc(sizeof(linkedListNotes_t));
	newNoteNode->index = notesStructure.index;
	newNoteNode->alteration = notesStructure.alteration;
	newNoteNode->octave = notesStructure.octave;
	newNoteNode->cents = notesStructure.cents;
	generateNoteName(notesTransferArray, notesStructure.index, notesStructure.alteration, notesStructure.octave);
	newNoteNode->name = (char*)malloc(strlen(notesTransferArray)+1);
	strcpy(newNoteNode->name, notesTransferArray);
	newNoteNode->next = NULL;
	
	*head = addNotesNodeToTail(*head, newNoteNode);
	
	nbrOfNoteNodes++;
}

}

void freeNotesList(linkedListNotes_t * head){

linkedListNotes_t * tmp = NULL;

while (head != NULL)
{
	tmp = head;
	head = head->next;
	tmp->next = NULL;
	free(tmp->name);
	tmp->name = NULL;
	free(tmp);
	tmp=NULL;
}

}

linkedListNotes_t * addNotesNodeToTail(linkedListNotes_t *head, linkedListNotes_t * newNode){

linkedListNotes_t * temp;

if (!head)
{
    head = newNode;
    return head;
}

temp = head;

while (temp->next)
	{
    temp = temp->next;
	}

temp->next = newNode;

return head;

}

Parents Reply Children
No Data
Related