I have a memory leak and the responsible frame is strdup. who can give me some clues?

I have a memory leak when I discover a tool. I do not have much experience in managing memory, so I can’t understand what is the possible cause of this problem, a memory leak looks like this:

enter image description here

enter image description here

I want to know the possible cause of such a memory leak. Can anyone give me some clues?

+4
source share
2 answers

strdup uses malloc internally, so anything strdup -ed had to be freed with free .

For instance:

 char *duplicate = strdup("abcdef"); ... free(duplicate); 
+2
source

strdup() is a library function, so you need to go back to backtrace until you find the caller in your code. There you will find the library call, as a result of which memory is allocated - it should have a corresponding release call in another place of your program.

(The release function is not necessarily a direct call to free() - for example, if you call the library function getaddrinfo() , the corresponding release function is freeaddrinfo() ).

+1
source

Source: https://habr.com/ru/post/1415086/


All Articles