AlexeyAB / darknet

YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object Detection (Windows and Linux version of Darknet )
http://pjreddie.com/darknet/
Other
21.76k stars 7.96k forks source link

free_network() does not deallocate the heap from load_network() #5537

Open jerry73204 opened 4 years ago

jerry73204 commented 4 years ago

As shown in the header, load_network() calls calloc() internally to allocate the space for struct network. However, free_network() takes a content copy instead of a struct 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 uninitialized network struct to load_network(), and the user manages the memory on his or her own.

imaami commented 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".