It’s not an easy job, careful design and consideration is needed to make memory allocation and free robust and efficiency. Here just go through the way Open SSL conducting memory leak. Here just give the brief instruction and architecture.
1, re-implement the memory allocate function,
2, use a hash table to record every allocation, restored in node mem_st.
3, use a trigger flag to control if monitors all the memory allocation. Such as CRYPTO_MEM_CHECK_ON and CRYPTO_MEM_CHECK_OFF.
4, have to use a memory lock to separate the inner and outer allocation. MemCheck_off() and MemCheck_on(), still it’s not very clear to meL.
5, the callback function CRYPTO_mem_leaks_cb() can be used to cleanup all the leak memory.
Why the OPEN SSL only have the function to printout all the leak memory, and don’t have the standard function to free them? Maybe it has to be implement by the users themselves.