• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: C memory leak, where there is no memory leak? (valgrind)

Resolved: C memory leak, where there is no memory leak? (valgrind)

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

valgrind is telling me that a specific line in my code creates a memory leak, but when looking at that line, it does not seem to even be able to create one.
I am working with this pretty simple Linked List struct list.h:
And this is how a list is initialized in list.c:
My queue works so, that the first object of the first linked_list is always NULL, the first object is stored in the next linked_list.
Now here is where the memory leak occurs:
This is what valgrind tells me:
Here is the function that recursively frees the list (no memory leak detected):

Answer:

You don’t free the last node in the list.
free_list does nothing if list->next is NULL. But you don’t want to do nothing. You want to not recurse, but you still need to free the node. So move the call to free out of the conditional, or change the test to check whether list itself is NULL

If you have better answer, please add a comment about this, thank you!

Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to make statement case insensitive when uploading a dta file with specified columns?

27/03/2023

Resolved: Sort dataframe columns value by close it to another column

27/03/2023

Resolved: PostgreSQL resample 1 minute OHLCV candle data into 5 minute OHLCV candle data

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.