Open jerry73204 opened 4 years ago
Darknet's design has a basic flaw with how structs are handled. Almost everything that returns a struct actually returns an on-stack copy, which is often both unhelpful and even harmful. Also, much of the heap memory allocs seem to be very small amounts of memory in member pointers of these structs.
It's as if the whole thing is designed with memory management as an afterthought.
The entire approach to layers should be changed from "copying around long lists of pointers to several small buffers" to "passing pointers to a few large buffers".
As shown in the header,
load_network()
callscalloc()
internally to allocate the space forstruct network
. However,free_network()
takes a content copy instead of astruct network*
pointer, leaving the heap allocated pointer behind. Users may accidentally dismiss it and leak the memory.There are two common ways to cope with it. One is to let
free_network()
accept a pointer and free it. The other way is to let user provide the uninitializednetwork
struct toload_network()
, and the user manages the memory on his or her own.