I have a List, each node in the list holds a char *data field which has to be malloc'd with the node itself, but when I try to free that data the console just stops, it doesn't crash, their is no error, nothing. But if I comment it out and only free the list node it works fine.
Am I doing this right?
void removeSpecificData(List *list, char *course)
{
ListNodePtr currentNode = list->head;
ListNodePtr previousNode = NULL;
while (currentNode != NULL)
{
if (strcmp(currentNode->data, course) == 0)
{
ListNodePtr nodeToFree = currentNode;
if (previousNode == NULL)
{
list->head = currentNode->nextNode;
currentNode = list->head;
}
else
{
previousNode->nextNode = currentNode->nextNode;
currentNode = previousNode->nextNode;
}
printf("Free Data");
free(nodeToFree->data);
printf("Free Node");
free(nodeToFree);
}
else
{
previousNode = currentNode;
currentNode = currentNode->nextNode;
}
}
}
Here's my createListNode function incase it's necessary to view.
ListNodePtr createListNode(char *newCourse)
{
ListNodePtr newNode = (ListNodePtr)malloc(sizeof(struct ListNode));
newNode->data = (char *)malloc(sizeof(strlen(newCourse) + 1));
strcpy(newNode->data, newCourse);
newNode->nextNode = NULL;
return newNode;
}
Thanks in advance.