From 3555beb91401cf746fd357b4029231143d00f58f Mon Sep 17 00:00:00 2001 From: AlexeyAB Date: Fri, 15 Nov 2019 22:51:06 +0300 Subject: [PATCH] Fixed some memory leaks in secondary functions --- src/classifier.c | 2 ++ src/coco.c | 10 ++++++- src/detector.c | 17 +++++++++++- src/gemm.c | 64 ++++++++++++++++++++++---------------------- src/go.c | 1 + src/image_opencv.cpp | 4 ++- src/network.c | 7 ++++- src/utils.c | 4 ++- src/yolo.c | 8 ++++++ 9 files changed, 80 insertions(+), 37 deletions(-) diff --git a/src/classifier.c b/src/classifier.c index c5f8e2f1..c077f61a 100644 --- a/src/classifier.c +++ b/src/classifier.c @@ -1288,4 +1288,6 @@ void run_classifier(int argc, char **argv) else if(0==strcmp(argv[2], "valid10")) validate_classifier_10(data, cfg, weights); else if(0==strcmp(argv[2], "validcrop")) validate_classifier_crop(data, cfg, weights); else if(0==strcmp(argv[2], "validfull")) validate_classifier_full(data, cfg, weights); + + if (gpus && gpu_list && ngpus > 1) free(gpus); } diff --git a/src/coco.c b/src/coco.c index cdfd3dff..03dd3a61 100644 --- a/src/coco.c +++ b/src/coco.c @@ -226,6 +226,12 @@ void validate_coco(char *cfgfile, char *weightfile) fprintf(fp, "\n]\n"); fclose(fp); + if (val) free(val); + if (val_resized) free(val_resized); + if (buf) free(buf); + if (buf_resized) free(buf_resized); + if (thr) free(thr); + fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)(time(0) - start)); } @@ -307,7 +313,9 @@ void validate_coco_recall(char *cfgfile, char *weightfile) } fprintf(stderr, "%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\n", i, correct, total, (float)proposals/(i+1), avg_iou*100/total, 100.*correct/total); - free(id); + + if (fps) free(fps); + if (id) free(id); free_image(orig); free_image(sized); } diff --git a/src/detector.c b/src/detector.c index efe5571a..7f32c4fa 100644 --- a/src/detector.c +++ b/src/detector.c @@ -556,6 +556,7 @@ void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *out for (j = 0; j < classes; ++j) { if (fps) fclose(fps[j]); } + if (fps) free(fps); if (coco) { #ifdef WIN32 fseek(fp, -3, SEEK_CUR); @@ -563,8 +564,15 @@ void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *out fseek(fp, -2, SEEK_CUR); #endif fprintf(fp, "\n]\n"); - fclose(fp); } + if (fp) fclose(fp); + + if (val) free(val); + if (val_resized) free(val_resized); + if (thr) free(thr); + if (buf) free(buf); + if (buf_resized) free(buf_resized); + fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)time(0) - start); } @@ -1099,6 +1107,11 @@ float validate_detector_map(char *datacfg, char *cfgfile, char *weightfile, floa else { free_network(net); } + if (val) free(val); + if (val_resized) free(val_resized); + if (thr) free(thr); + if (buf) free(buf); + if (buf_resized) free(buf_resized); return mean_average_precision; } @@ -1505,4 +1518,6 @@ void run_detector(int argc, char **argv) free_list(options); } else printf(" There isn't such command: %s", argv[2]); + + if (gpus && gpu_list && ngpus > 1) free(gpus); } diff --git a/src/gemm.c b/src/gemm.c index 9f5cb882..51f77cac 100644 --- a/src/gemm.c +++ b/src/gemm.c @@ -324,7 +324,7 @@ void transpose_32x32_bits_my(uint32_t *A, uint32_t *B, int lda, int ldb) unsigned int x, y; for (y = 0; y < 32; ++y) { for (x = 0; x < 32; ++x) { - if (A[y * lda] & (1 << x)) B[x * ldb] |= (uint32_t)1 << y; + if (A[y * lda] & ((uint32_t)1 << x)) B[x * ldb] |= (uint32_t)1 << y; } } } @@ -636,48 +636,48 @@ void check_cpu_features(void) { // Detect Features if (nIds >= 0x00000001) { cpuid(info, 0x00000001); - HW_MMX = (info[3] & ((int)1 << 23)) != 0; - HW_SSE = (info[3] & ((int)1 << 25)) != 0; - HW_SSE2 = (info[3] & ((int)1 << 26)) != 0; - HW_SSE3 = (info[2] & ((int)1 << 0)) != 0; + HW_MMX = (info[3] & ((uint32_t)1 << 23)) != 0; + HW_SSE = (info[3] & ((uint32_t)1 << 25)) != 0; + HW_SSE2 = (info[3] & ((uint32_t)1 << 26)) != 0; + HW_SSE3 = (info[2] & ((uint32_t)1 << 0)) != 0; - HW_SSSE3 = (info[2] & ((int)1 << 9)) != 0; - HW_SSE41 = (info[2] & ((int)1 << 19)) != 0; - HW_SSE42 = (info[2] & ((int)1 << 20)) != 0; - HW_AES = (info[2] & ((int)1 << 25)) != 0; + HW_SSSE3 = (info[2] & ((uint32_t)1 << 9)) != 0; + HW_SSE41 = (info[2] & ((uint32_t)1 << 19)) != 0; + HW_SSE42 = (info[2] & ((uint32_t)1 << 20)) != 0; + HW_AES = (info[2] & ((uint32_t)1 << 25)) != 0; - HW_AVX = (info[2] & ((int)1 << 28)) != 0; - HW_FMA3 = (info[2] & ((int)1 << 12)) != 0; + HW_AVX = (info[2] & ((uint32_t)1 << 28)) != 0; + HW_FMA3 = (info[2] & ((uint32_t)1 << 12)) != 0; - HW_RDRAND = (info[2] & ((int)1 << 30)) != 0; + HW_RDRAND = (info[2] & ((uint32_t)1 << 30)) != 0; } if (nIds >= 0x00000007) { cpuid(info, 0x00000007); - HW_AVX2 = (info[1] & ((int)1 << 5)) != 0; + HW_AVX2 = (info[1] & ((uint32_t)1 << 5)) != 0; - HW_BMI1 = (info[1] & ((int)1 << 3)) != 0; - HW_BMI2 = (info[1] & ((int)1 << 8)) != 0; - HW_ADX = (info[1] & ((int)1 << 19)) != 0; - HW_SHA = (info[1] & ((int)1 << 29)) != 0; - HW_PREFETCHWT1 = (info[2] & ((int)1 << 0)) != 0; + HW_BMI1 = (info[1] & ((uint32_t)1 << 3)) != 0; + HW_BMI2 = (info[1] & ((uint32_t)1 << 8)) != 0; + HW_ADX = (info[1] & ((uint32_t)1 << 19)) != 0; + HW_SHA = (info[1] & ((uint32_t)1 << 29)) != 0; + HW_PREFETCHWT1 = (info[2] & ((uint32_t)1 << 0)) != 0; - HW_AVX512F = (info[1] & ((int)1 << 16)) != 0; - HW_AVX512CD = (info[1] & ((int)1 << 28)) != 0; - HW_AVX512PF = (info[1] & ((int)1 << 26)) != 0; - HW_AVX512ER = (info[1] & ((int)1 << 27)) != 0; - HW_AVX512VL = (info[1] & ((int)1 << 31)) != 0; - HW_AVX512BW = (info[1] & ((int)1 << 30)) != 0; - HW_AVX512DQ = (info[1] & ((int)1 << 17)) != 0; - HW_AVX512IFMA = (info[1] & ((int)1 << 21)) != 0; - HW_AVX512VBMI = (info[2] & ((int)1 << 1)) != 0; + HW_AVX512F = (info[1] & ((uint32_t)1 << 16)) != 0; + HW_AVX512CD = (info[1] & ((uint32_t)1 << 28)) != 0; + HW_AVX512PF = (info[1] & ((uint32_t)1 << 26)) != 0; + HW_AVX512ER = (info[1] & ((uint32_t)1 << 27)) != 0; + HW_AVX512VL = (info[1] & ((uint32_t)1 << 31)) != 0; + HW_AVX512BW = (info[1] & ((uint32_t)1 << 30)) != 0; + HW_AVX512DQ = (info[1] & ((uint32_t)1 << 17)) != 0; + HW_AVX512IFMA = (info[1] & ((uint32_t)1 << 21)) != 0; + HW_AVX512VBMI = (info[2] & ((uint32_t)1 << 1)) != 0; } if (nExIds >= 0x80000001) { cpuid(info, 0x80000001); - HW_x64 = (info[3] & ((int)1 << 29)) != 0; - HW_ABM = (info[2] & ((int)1 << 5)) != 0; - HW_SSE4a = (info[2] & ((int)1 << 6)) != 0; - HW_FMA4 = (info[2] & ((int)1 << 16)) != 0; - HW_XOP = (info[2] & ((int)1 << 11)) != 0; + HW_x64 = (info[3] & ((uint32_t)1 << 29)) != 0; + HW_ABM = (info[2] & ((uint32_t)1 << 5)) != 0; + HW_SSE4a = (info[2] & ((uint32_t)1 << 6)) != 0; + HW_FMA4 = (info[2] & ((uint32_t)1 << 16)) != 0; + HW_XOP = (info[2] & ((uint32_t)1 << 11)) != 0; } } diff --git a/src/go.c b/src/go.c index 5d507768..88da6c0d 100644 --- a/src/go.c +++ b/src/go.c @@ -47,6 +47,7 @@ moves load_go_moves(char *filename) printf("%d\n", count); m.n = count; m.data = (char**)realloc(m.data, count * sizeof(char*)); + fclose(fp); return m; } diff --git a/src/image_opencv.cpp b/src/image_opencv.cpp index 6951fb9a..0a8ccd98 100644 --- a/src/image_opencv.cpp +++ b/src/image_opencv.cpp @@ -703,11 +703,12 @@ int set_capture_position_frame_cv(cap_cv *cap, int index) image get_image_from_stream_cpp(cap_cv *cap) { - cv::Mat *src = new cv::Mat(); + cv::Mat *src = NULL; static int once = 1; if (once) { once = 0; do { + if (src) delete src; src = get_capture_frame_cv(cap); if (!src) return make_empty_image(0, 0, 0); } while (src->cols < 1 || src->rows < 1 || src->channels() < 1); @@ -719,6 +720,7 @@ image get_image_from_stream_cpp(cap_cv *cap) if (!src) return make_empty_image(0, 0, 0); image im = mat_to_image(*src); rgbgr_image(im); + if (src) delete src; return im; } // ---------------------------------------- diff --git a/src/network.c b/src/network.c index c2249a54..15c9a9bc 100644 --- a/src/network.c +++ b/src/network.c @@ -809,6 +809,7 @@ char *detection_to_json(detection *dets, int nboxes, int classes, char **names, const float thresh = 0.005; // function get_network_boxes() has already filtred dets by actual threshold char *send_buf = (char *)calloc(1024, sizeof(char)); + if (!send_buf) return 0; if (filename) { sprintf(send_buf, "{\n \"frame_id\":%lld, \n \"filename\":\"%s\", \n \"objects\": [ \n", frame_id, filename); } @@ -826,6 +827,7 @@ char *detection_to_json(detection *dets, int nboxes, int classes, char **names, if (class_id != -1) strcat(send_buf, ", \n"); class_id = j; char *buf = (char *)calloc(2048, sizeof(char)); + if (!buf) return 0; //sprintf(buf, "{\"image_id\":%d, \"category_id\":%d, \"bbox\":[%f, %f, %f, %f], \"score\":%f}", // image_id, j, dets[i].bbox.x, dets[i].bbox.y, dets[i].bbox.w, dets[i].bbox.h, dets[i].prob[j]); @@ -836,7 +838,10 @@ char *detection_to_json(detection *dets, int nboxes, int classes, char **names, int buf_len = strlen(buf); int total_len = send_buf_len + buf_len + 100; send_buf = (char *)realloc(send_buf, total_len * sizeof(char)); - if (!send_buf) return 0;// exit(-1); + if (!send_buf) { + if (buf) free(buf); + return 0;// exit(-1); + } strcat(send_buf, buf); free(buf); } diff --git a/src/utils.c b/src/utils.c index bee427ed..f18769ce 100644 --- a/src/utils.c +++ b/src/utils.c @@ -41,6 +41,7 @@ int *read_map(char *filename) map = (int*)realloc(map, n * sizeof(int)); map[n-1] = atoi(str); } + if (file) fclose(file); return map; } @@ -65,6 +66,7 @@ void shuffle(void *arr, size_t n, size_t size) memcpy((char*)arr+(j*size), (char*)arr+(i*size), size); memcpy((char*)arr+(i*size), swp, size); } + free(swp); } void del_arg(int argc, char **argv, int index) @@ -685,9 +687,9 @@ int max_index(float *a, int n) int top_max_index(float *a, int n, int k) { + if (n <= 0) return -1; float *values = (float*)calloc(k, sizeof(float)); int *indexes = (int*)calloc(k, sizeof(int)); - if (n <= 0) return -1; int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < k; ++j) { diff --git a/src/yolo.c b/src/yolo.c index 711470ea..339d49cd 100644 --- a/src/yolo.c +++ b/src/yolo.c @@ -189,6 +189,14 @@ void validate_yolo(char *cfgfile, char *weightfile) free_image(val_resized[t]); } } + + if (fps) free(fps); + if (val) free(val); + if (val_resized) free(val_resized); + if (buf) free(buf); + if (buf_resized) free(buf_resized); + if (thr) free(thr); + fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)(time(0) - start)); }