AlexeyAB / Yolo_mark

GUI for marking bounded boxes of objects in images for training neural network Yolo v3 and v2
https://github.com/AlexeyAB/darknet
The Unlicense
1.8k stars 680 forks source link

two memory leaking #89

Closed zhenglinwang closed 6 years ago

zhenglinwang commented 6 years ago

So far I found two memory leaking: (1) data.c: line 21 list get_paths(char filename) { char path; FILE file = fopen(filename, "r");

//printf("Error %d \n", errno);
if(!file) file_error(filename);
list *lines = make_list();
while((path=fgetl(file))){
    if (strlen(path) == 0)

// possible memory leaking for "path"

{ free(path); continue; }

    list_insert(lines, path);
}
fclose(file);
return lines;

}

(2) detector.c around line 1160 image raw_im = load_image(filename, 0, 0, myNet.c); embed_image(raw_im, im, 0, 0);

// raw_im should be free as it allocates memory

free_image(raw_im);

AlexeyAB commented 6 years ago

It isn't related to this repository. But let's look at the Darknet repository: https://github.com/AlexeyAB/darknet


https://github.com/AlexeyAB/darknet/blob/57e878b4f9512cf9995ff6b5cd6e0d7dc1da9eaf/src/data.c#L12-L23

(1) data.c: line 21

list *get_paths(char *filename)
{
    char *path;
    FILE *file = fopen(filename, "r");
    if(!file) file_error(filename);
    list *lines = make_list();
    while((path=fgetl(file))){
        list_insert(lines, path);
    }
    fclose(file);
    return lines;
}

The pointer path will be stored to the list *lines and will be used outside of this function:

Later these pointers will be copied to the array of pointers char **paths = (char **)list_to_array(plist); https://github.com/AlexeyAB/darknet/blob/57e878b4f9512cf9995ff6b5cd6e0d7dc1da9eaf/src/detector.c#L84

All these structures will be freed there, at the end of training function: https://github.com/AlexeyAB/darknet/blob/57e878b4f9512cf9995ff6b5cd6e0d7dc1da9eaf/src/detector.c#L234-L236

    free(paths);
    free_list_contents(plist);
    free_list(plist);

(2) detector.c around line 1160

image raw_im = load_image(filename, 0, 0, myNet.c);
embed_image(raw_im, im, 0, 0);

// raw_im should be free as it allocates memory

free_image(raw_im);

There is no such line image raw_im = load_image(filename, 0, 0, myNet.c); in my repository at all: https://github.com/AlexeyAB/darknet/blob/57e878b4f9512cf9995ff6b5cd6e0d7dc1da9eaf/src/detector.c#L1160

Original repository doesn't have such line too: https://github.com/pjreddie/darknet/blob/master/examples/detector.c

zhenglinwang commented 6 years ago

Thanks! My version is a bit old. Download and test it again.

zhenglinwang commented 6 years ago

Sorry. Actually the memory leaking was introduced by my code.