From 7ac84100498616457c97839f35afd78d3fce846f Mon Sep 17 00:00:00 2001 From: Mr-Dave Date: Tue, 3 May 2022 21:26:04 -0600 Subject: [PATCH] Revise free then null steps to be a single function --- src/alg_sec.cpp | 12 +--- src/conf.cpp | 5 +- src/dbse.cpp | 36 +++--------- src/event.cpp | 30 ++-------- src/exif.cpp | 5 +- src/mmalcam.cpp | 12 +--- src/motion_loop.cpp | 135 ++++++++------------------------------------ src/motionplus.cpp | 10 +--- src/movie.cpp | 40 +++---------- src/netcam.cpp | 47 ++++----------- src/rotate.cpp | 18 +----- src/util.cpp | 26 ++++----- src/util.hpp | 1 + src/video_v4l2.cpp | 62 ++++---------------- src/webu.cpp | 104 +++++++--------------------------- src/webu_stream.cpp | 45 +++------------ 16 files changed, 122 insertions(+), 466 deletions(-) diff --git a/src/alg_sec.cpp b/src/alg_sec.cpp index 54682228..6c1d92cf 100644 --- a/src/alg_sec.cpp +++ b/src/alg_sec.cpp @@ -389,10 +389,7 @@ static void algsec_params_deinit(ctx_algsec_model &algmdl) { if (algmdl.algsec_params != NULL){ util_parms_free(algmdl.algsec_params); - if (algmdl.algsec_params != NULL) { - free(algmdl.algsec_params); - algmdl.algsec_params = NULL; - } + myfree(&algmdl.algsec_params); } } @@ -561,12 +558,7 @@ void algsec_deinit(ctx_cam *cam) waitcnt++; } } - if (cam->algsec->image_norm != NULL){ - if (cam->algsec->image_norm != NULL) { - free(cam->algsec->image_norm); - cam->algsec->image_norm = NULL; - } - } + myfree(&cam->algsec->image_norm); if (waitcnt == 1000){ MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO diff --git a/src/conf.cpp b/src/conf.cpp index 537ccb5d..28d91fb1 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -4107,10 +4107,7 @@ void conf_deinit(struct ctx_motapp *motapp) indx++; } - if (motapp->cam_list != NULL) { - free(motapp->cam_list); - motapp->cam_list = NULL; - } + myfree(&motapp->cam_list); } diff --git a/src/dbse.cpp b/src/dbse.cpp index dcc483a4..04a23339 100644 --- a/src/dbse.cpp +++ b/src/dbse.cpp @@ -82,10 +82,7 @@ void dbse_global_deinit(struct ctx_motapp *motapp) indx = 0; while (motapp->cam_list[indx] != NULL) { - if (motapp->cam_list[indx]->dbse != NULL) { - free(motapp->cam_list[indx]->dbse); - motapp->cam_list[indx]->dbse = NULL; - } + myfree(&motapp->cam_list[indx]->dbse); indx++; } @@ -767,21 +764,11 @@ static void dbse_motpls_free_movies(struct ctx_cam *cam) if (cam->dbsemp->movie_list != NULL) { for (indx=0; indxdbsemp->movie_cnt; indx++) { - if (cam->dbsemp->movie_list[indx].movie_nm != NULL) { - free(cam->dbsemp->movie_list[indx].movie_nm); - cam->dbsemp->movie_list[indx].movie_nm = NULL; - } - if (cam->dbsemp->movie_list[indx].full_nm != NULL) { - free(cam->dbsemp->movie_list[indx].full_nm); - cam->dbsemp->movie_list[indx].full_nm = NULL; - } - if (cam->dbsemp->movie_list[indx].movie_tmc != NULL) { - free(cam->dbsemp->movie_list[indx].movie_tmc); - cam->dbsemp->movie_list[indx].movie_tmc = NULL; - } + myfree(&cam->dbsemp->movie_list[indx].movie_nm); + myfree(&cam->dbsemp->movie_list[indx].full_nm); + myfree(&cam->dbsemp->movie_list[indx].movie_tmc); } - free(cam->dbsemp->movie_list); - cam->dbsemp->movie_list = NULL; + myfree(&cam->dbsemp->movie_list); } cam->dbsemp->movie_cnt = 0; @@ -798,17 +785,10 @@ static void dbse_motpls_free_cols(struct ctx_cam *cam) if (cam->dbsemp->cols_list != NULL) { for (indx=0; indxdbsemp->cols_cnt; indx++) { - if (cam->dbsemp->cols_list[indx].col_nm != NULL) { - free(cam->dbsemp->cols_list[indx].col_nm); - cam->dbsemp->cols_list[indx].col_nm = NULL; - } - if (cam->dbsemp->cols_list[indx].col_typ != NULL) { - free(cam->dbsemp->cols_list[indx].col_typ); - cam->dbsemp->cols_list[indx].col_typ = NULL; - } + myfree(&cam->dbsemp->cols_list[indx].col_nm); + myfree(&cam->dbsemp->cols_list[indx].col_typ); } - free(cam->dbsemp->cols_list); - cam->dbsemp->cols_list = NULL; + myfree(&cam->dbsemp->cols_list); } cam->dbsemp->cols_cnt = 0; diff --git a/src/event.cpp b/src/event.cpp index c94c005d..a2fbc03d 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -603,10 +603,7 @@ static void event_movie_start(struct ctx_cam *cam, motion_event evnt if (retcd < 0) { MOTION_LOG(ERR, TYPE_EVENTS, NO_ERRNO ,_("Error opening ctx_cam for movie output.")); - if (cam->movie_norm != NULL) { - free(cam->movie_norm); - cam->movie_norm = NULL; - } + myfree(&cam->movie_norm); return; } event(cam, EVENT_FILECREATE, NULL, cam->movie_norm->filename, (void *)FTYPE_MOVIE, ts1); @@ -618,10 +615,7 @@ static void event_movie_start(struct ctx_cam *cam, motion_event evnt if (retcd < 0) { MOTION_LOG(ERR, TYPE_EVENTS, NO_ERRNO ,_("Error creating motion file [%s]"), cam->movie_motion->filename); - if (cam->movie_motion != NULL) { - free(cam->movie_motion); - cam->movie_motion = NULL; - } + myfree(&cam->movie_motion); return; } } @@ -659,10 +653,7 @@ static void event_movie_end(struct ctx_cam *cam, motion_event evnt if (cam->movie_norm) { movie_close(cam->movie_norm); - if (cam->movie_norm != NULL) { - free(cam->movie_norm); - cam->movie_norm = NULL; - } + myfree(&cam->movie_norm); if ((cam->conf->movie_retain == "secondary") && (cam->algsec_inuse)) { if (cam->algsec->isdetected == false) { @@ -685,10 +676,7 @@ static void event_movie_end(struct ctx_cam *cam, motion_event evnt if (cam->movie_motion) { movie_close(cam->movie_motion); - if (cam->movie_motion != NULL) { - free(cam->movie_motion); - cam->movie_motion = NULL; - } + myfree(&cam->movie_motion); if ((cam->conf->movie_retain == "secondary") && (cam->algsec_inuse)) { if (cam->algsec->isdetected == false) { @@ -726,10 +714,7 @@ static void event_tlapse_start(struct ctx_cam *cam, motion_event evnt if (retcd < 0) { MOTION_LOG(ERR, TYPE_EVENTS, NO_ERRNO ,_("Error creating timelapse file [%s]"), cam->movie_timelapse->filename); - if (cam->movie_timelapse != NULL) { - free(cam->movie_timelapse); - cam->movie_timelapse = NULL; - } + myfree(&cam->movie_timelapse); return; } event(cam, EVENT_FILECREATE, NULL, cam->movie_timelapse->filename @@ -755,10 +740,7 @@ static void event_tlapse_end(struct ctx_cam *cam, motion_event evnt if (cam->movie_timelapse) { movie_close(cam->movie_timelapse); - if (cam->movie_timelapse != NULL) { - free(cam->movie_timelapse); - cam->movie_timelapse = NULL; - } + myfree(&cam->movie_timelapse); event(cam, EVENT_FILECLOSE, NULL, cam->timelapsefilename, (void *)FTYPE_MOVIE_TIMELAPSE, ts1); dbse_exec(cam, cam->timelapsefilename, FTYPE_MOVIE_TIMELAPSE, ts1, "movie_end"); } diff --git a/src/exif.cpp b/src/exif.cpp index 0e2afa6a..bacad750 100644 --- a/src/exif.cpp +++ b/src/exif.cpp @@ -345,10 +345,7 @@ unsigned exif_prepare(unsigned char **exif, const struct ctx_cam *cam, /* assert we didn't underestimate the original buffer size */ assert(marker_len <= buffer_size); - if (description != NULL) { - free(description); - description = NULL; - } + myfree(&description); *exif = marker; return marker_len; diff --git a/src/mmalcam.cpp b/src/mmalcam.cpp index 4acc8fdb..8120b493 100644 --- a/src/mmalcam.cpp +++ b/src/mmalcam.cpp @@ -381,16 +381,8 @@ void mmalcam_cleanup(struct ctx_mmalcam *mmalcam) destroy_camera_buffer_structures(mmalcam); destroy_camera_component(mmalcam); } - - if (mmalcam->camera_parameters != NULL) { - free(mmalcam->camera_parameters); - mmalcam->camera_parameters = NULL; - } - - if (mmalcam != NULL) { - free(mmalcam); - mmalcam = NULL; - } + myfree(&mmalcam->camera_parameters); + myfree(&mmalcam); } #else (void)mmalcam; diff --git a/src/motion_loop.cpp b/src/motion_loop.cpp index ec71a144..aea54ef9 100644 --- a/src/motion_loop.cpp +++ b/src/motion_loop.cpp @@ -74,10 +74,7 @@ static void mlp_ring_resize(struct ctx_cam *cam, int new_size) } } - if (cam->imgs.image_ring != NULL) { - free(cam->imgs.image_ring); - cam->imgs.image_ring = NULL; - } + myfree(&cam->imgs.image_ring); cam->imgs.image_ring = tmp; cam->current_image = NULL; @@ -99,21 +96,10 @@ static void mlp_ring_destroy(struct ctx_cam *cam) } for (i = 0; i < cam->imgs.ring_size; i++) { - if (cam->imgs.image_ring[i].image_norm != NULL) { - free(cam->imgs.image_ring[i].image_norm); - cam->imgs.image_ring[i].image_norm = NULL; - } - - if (cam->imgs.image_ring[i].image_high != NULL) { - free(cam->imgs.image_ring[i].image_high); - cam->imgs.image_ring[i].image_high = NULL; - } - - } - if (cam->imgs.image_ring != NULL) { - free(cam->imgs.image_ring); - cam->imgs.image_ring = NULL; + myfree(&cam->imgs.image_ring[i].image_norm); + myfree(&cam->imgs.image_ring[i].image_high); } + myfree(&cam->imgs.image_ring); /* * current_image is an alias from the pointers above which have @@ -781,100 +767,25 @@ void mlp_cleanup(struct ctx_cam *cam) mlp_cam_close(cam); } - if (cam->imgs.image_motion.image_norm != NULL) { - free(cam->imgs.image_motion.image_norm); - cam->imgs.image_motion.image_norm = NULL; - } - - if (cam->imgs.ref != NULL) { - free(cam->imgs.ref); - cam->imgs.ref = NULL; - } - - if (cam->imgs.ref_dyn != NULL) { - free(cam->imgs.ref_dyn); - cam->imgs.ref_dyn = NULL; - } - - if (cam->imgs.image_virgin != NULL) { - free(cam->imgs.image_virgin); - cam->imgs.image_virgin = NULL; - } - - if (cam->imgs.image_vprvcy != NULL) { - free(cam->imgs.image_vprvcy); - cam->imgs.image_vprvcy = NULL; - } - - if (cam->imgs.labels != NULL) { - free(cam->imgs.labels); - cam->imgs.labels = NULL; - } - - if (cam->imgs.labelsize != NULL) { - free(cam->imgs.labelsize); - cam->imgs.labelsize = NULL; - } - - if (cam->imgs.smartmask != NULL) { - free(cam->imgs.smartmask); - cam->imgs.smartmask = NULL; - } - - if (cam->imgs.smartmask_final != NULL) { - free(cam->imgs.smartmask_final); - cam->imgs.smartmask_final = NULL; - } - - if (cam->imgs.smartmask_buffer != NULL) { - free(cam->imgs.smartmask_buffer); - cam->imgs.smartmask_buffer = NULL; - } - - if (cam->imgs.mask != NULL) { - free(cam->imgs.mask); - cam->imgs.mask = NULL; - } - - if (cam->imgs.mask_privacy != NULL) { - free(cam->imgs.mask_privacy); - cam->imgs.mask_privacy = NULL; - } - - if (cam->imgs.mask_privacy_uv != NULL) { - free(cam->imgs.mask_privacy_uv); - cam->imgs.mask_privacy_uv = NULL; - } - - if (cam->imgs.mask_privacy_high != NULL) { - free(cam->imgs.mask_privacy_high); - cam->imgs.mask_privacy_high = NULL; - } - - if (cam->imgs.mask_privacy_high_uv != NULL) { - free(cam->imgs.mask_privacy_high_uv); - cam->imgs.mask_privacy_high_uv = NULL; - } - - if (cam->imgs.common_buffer != NULL) { - free(cam->imgs.common_buffer); - cam->imgs.common_buffer = NULL; - } - - if (cam->imgs.image_secondary != NULL) { - free(cam->imgs.image_secondary); - cam->imgs.image_secondary = NULL; - } - - if (cam->imgs.image_preview.image_norm != NULL) { - free(cam->imgs.image_preview.image_norm); - cam->imgs.image_preview.image_norm = NULL; - } - - if (cam->imgs.image_preview.image_high != NULL) { - free(cam->imgs.image_preview.image_high); - cam->imgs.image_preview.image_high = NULL; - } + myfree(&cam->imgs.image_motion.image_norm); + myfree(&cam->imgs.ref); + myfree(&cam->imgs.ref_dyn); + myfree(&cam->imgs.image_virgin); + myfree(&cam->imgs.image_vprvcy); + myfree(&cam->imgs.labels); + myfree(&cam->imgs.labelsize); + myfree(&cam->imgs.smartmask); + myfree(&cam->imgs.smartmask_final); + myfree(&cam->imgs.smartmask_buffer); + myfree(&cam->imgs.mask); + myfree(&cam->imgs.mask_privacy); + myfree(&cam->imgs.mask_privacy_uv); + myfree(&cam->imgs.mask_privacy_high); + myfree(&cam->imgs.mask_privacy_high_uv); + myfree(&cam->imgs.common_buffer); + myfree(&cam->imgs.image_secondary); + myfree(&cam->imgs.image_preview.image_norm); + myfree(&cam->imgs.image_preview.image_high); mlp_ring_destroy(cam); /* Cleanup the precapture ring buffer */ diff --git a/src/motionplus.cpp b/src/motionplus.cpp index 71d8678f..15d43f6e 100644 --- a/src/motionplus.cpp +++ b/src/motionplus.cpp @@ -694,10 +694,7 @@ static void motion_cam_delete(struct ctx_motapp *motapp) return; } - if (motapp->cam_list[motapp->cam_delete]->dbse != NULL) { - free(motapp->cam_list[motapp->cam_delete]->dbse); - motapp->cam_list[motapp->cam_delete]->dbse = NULL; - } + myfree(&motapp->cam_list[motapp->cam_delete]->dbse); dbse_motpls_deinit(motapp->cam_list[motapp->cam_delete]); @@ -722,10 +719,7 @@ static void motion_cam_delete(struct ctx_motapp *motapp) /* Swap out the old list with the new */ pthread_mutex_lock(&motapp->mutex_camlst); - if (motapp->cam_list != NULL) { - free(motapp->cam_list); - motapp->cam_list = NULL; - } + myfree(&motapp->cam_list); motapp->cam_list = tmp; pthread_mutex_unlock(&motapp->mutex_camlst); diff --git a/src/movie.cpp b/src/movie.cpp index c262d3b5..c466953f 100644 --- a/src/movie.cpp +++ b/src/movie.cpp @@ -151,10 +151,7 @@ static int movie_get_oformat(struct ctx_movie *movie) MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Error setting base file name")); movie_free_context(movie); - if (container_name != NULL) { - free(container_name); - container_name = NULL; - } + myfree(&container_name); return -1; } @@ -170,16 +167,10 @@ static int movie_get_oformat(struct ctx_movie *movie) MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Error setting timelapse append for container %s"), container_name); movie_free_context(movie); - if (container_name != NULL) { - free(container_name); - container_name = NULL; - } + myfree(&container_name); return -1; } - if (container_name != NULL) { - free(container_name); - container_name = NULL; - } + myfree(&container_name); return 0; } @@ -236,10 +227,7 @@ static int movie_get_oformat(struct ctx_movie *movie) MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Error setting file name")); movie_free_context(movie); - if (container_name != NULL) { - free(container_name); - container_name = NULL; - } + myfree(&container_name); return -1; } @@ -247,27 +235,18 @@ static int movie_get_oformat(struct ctx_movie *movie) MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("container option value %s is not supported"), container_name); movie_free_context(movie); - if (container_name != NULL) { - free(container_name); - container_name = NULL; - } + myfree(&container_name); return -1; } if (movie->oc->oformat->video_codec == MY_CODEC_ID_NONE) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Could not get the container")); movie_free_context(movie); - if (container_name != NULL) { - free(container_name); - container_name = NULL; - } + myfree(&container_name); return -1; } - if (container_name != NULL) { - free(container_name); - container_name = NULL; - } + myfree(&container_name); return 0; } @@ -365,10 +344,7 @@ static int movie_encode_video(struct ctx_movie *movie) movie->pkt->pts = movie->picture->pts; movie->pkt->dts = movie->pkt->pts; - if (video_outbuf != NULL) { - free(video_outbuf); - video_outbuf = NULL; - } + myfree(&video_outbuf); /* This kills compiler warnings. Nal setting is only for recent movie versions*/ if (movie->preferred_codec == USER_CODEC_V4L2M2M) { diff --git a/src/netcam.cpp b/src/netcam.cpp index 4e58aebe..ef1360d4 100644 --- a/src/netcam.cpp +++ b/src/netcam.cpp @@ -251,17 +251,10 @@ static void netcam_url_parse(struct url_t *parse_url, const char *text_url) */ static void netcam_url_free(struct url_t *parse_url) { - free(parse_url->service); - parse_url->service = NULL; - - free(parse_url->userpass); - parse_url->userpass = NULL; - - free(parse_url->host); - parse_url->host = NULL; - - free(parse_url->path); - parse_url->path = NULL; + myfree(&parse_url->service); + myfree(&parse_url->userpass); + myfree(&parse_url->host); + myfree(&parse_url->path); } static void netcam_free_pkt(struct ctx_netcam *netcam) @@ -296,8 +289,7 @@ static void netcam_pktarray_free(struct ctx_netcam *netcam) netcam->pktarray[indx].packet = NULL; } } - free(netcam->pktarray); - netcam->pktarray = NULL; + myfree(&netcam->pktarray); netcam->pktarray_size = 0; netcam->pktarray_index = -1; pthread_mutex_unlock(&netcam->mutex_pktarray); @@ -394,9 +386,7 @@ static void netcam_pktarray_resize(struct ctx_cam *cam, bool is_highres) tmp[indx].iswritten = false; } - if (netcam->pktarray != NULL) { - free(netcam->pktarray); - } + myfree(&netcam->pktarray); netcam->pktarray = tmp; netcam->pktarray_size = newsize; @@ -753,7 +743,7 @@ static void netcam_decoder_error(struct ctx_netcam *netcam, int retcd, const cha for (indx = 0; indx < netcam->params->params_count; indx++) { if (mystreq(netcam->params->params_array[indx].param_name,"decoder") ) { - free(netcam->params->params_array[indx].param_value); + myfree(&netcam->params->params_array[indx].param_value); netcam->params->params_array[indx].param_value = (char*)mymalloc(5); snprintf(netcam->params->params_array[indx].param_value, 5, "%s","NULL"); break; @@ -766,7 +756,7 @@ static void netcam_decoder_error(struct ctx_netcam *netcam, int retcd, const cha util_parms_update(netcam->params, netcam->conf->netcam_params); } - free(netcam->decoder_nm); + myfree(&netcam->decoder_nm); netcam->decoder_nm = (char*)mymalloc(5); snprintf(netcam->decoder_nm, 5, "%s","NULL"); } @@ -1884,10 +1874,7 @@ static void netcam_shutdown(struct ctx_netcam *netcam) if (netcam) { netcam_close_context(netcam); - if (netcam->path != NULL) { - free(netcam->path); - netcam->path = NULL; - } + myfree(&netcam->path); if (netcam->img_latest != NULL) { free(netcam->img_latest->ptr); @@ -1901,16 +1888,9 @@ static void netcam_shutdown(struct ctx_netcam *netcam) netcam->img_recv = NULL; } - if (netcam->decoder_nm != NULL) { - free(netcam->decoder_nm); - netcam->decoder_nm = NULL; - } - + myfree(&netcam->decoder_nm); util_parms_free(netcam->params); - if (netcam->params != NULL) { - free(netcam->params); - netcam->params = NULL; - } + myfree(&netcam->params); } } @@ -2304,10 +2284,7 @@ void netcam_cleanup(struct ctx_cam *cam, bool init_retry_flag) pthread_mutex_destroy(&netcam->mutex_pktarray); pthread_mutex_destroy(&netcam->mutex_transfer); - if (netcam != NULL) { - free(netcam); - netcam = NULL; - } + myfree(&netcam); if (indx_cam == 1) { MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO diff --git a/src/rotate.cpp b/src/rotate.cpp index a67e86ba..385bb422 100644 --- a/src/rotate.cpp +++ b/src/rotate.cpp @@ -278,21 +278,9 @@ void rotate_deinit(struct ctx_cam *cam) if (cam->rotate_data == NULL) { return; } - - if (cam->rotate_data->buffer_norm != NULL) { - free(cam->rotate_data->buffer_norm); - cam->rotate_data->buffer_norm = NULL; - } - - if (cam->rotate_data->buffer_high != NULL) { - free(cam->rotate_data->buffer_high); - cam->rotate_data->buffer_high = NULL; - } - - if (cam->rotate_data != NULL) { - free(cam->rotate_data); - cam->rotate_data = NULL; - } + myfree(&cam->rotate_data->buffer_norm); + myfree(&cam->rotate_data->buffer_high); + myfree(&cam->rotate_data); } /** diff --git a/src/util.cpp b/src/util.cpp index 495bd677..b9f2cbf2 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -103,6 +103,16 @@ void mytrim(std::string &parm) myltrim(parm); } +/* Free memory and set the pointer to NULL*/ +void myfree(void *ptr_addr) { + void **ptr = (void **)ptr_addr; + + if (*ptr != NULL) { + free(*ptr); + *ptr = NULL; + } +} + /** mymalloc */ void *mymalloc(size_t nbytes) { @@ -779,20 +789,10 @@ void util_parms_free(struct ctx_params *params) } for (indx_parm=0; indx_parmparams_count; indx_parm++) { - if (params->params_array[indx_parm].param_name != NULL) { - free(params->params_array[indx_parm].param_name); - params->params_array[indx_parm].param_name = NULL; - } - if (params->params_array[indx_parm].param_value != NULL) { - free(params->params_array[indx_parm].param_value); - params->params_array[indx_parm].param_value = NULL; - } - } - - if (params->params_array != NULL) { - free(params->params_array); - params->params_array = NULL; + myfree(¶ms->params_array[indx_parm].param_name); + myfree(¶ms->params_array[indx_parm].param_value); } + myfree(¶ms->params_array); params->params_count = 0; diff --git a/src/util.hpp b/src/util.hpp index d7b49286..6737acc6 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -99,6 +99,7 @@ typedef int mhdrslt; /* Version independent return result from MHD */ #endif + void myfree(void *ptr_addr); void *mymalloc(size_t nbytes); void *myrealloc(void *ptr, size_t size, const char *desc); diff --git a/src/video_v4l2.cpp b/src/video_v4l2.cpp index bca57d9e..50442e67 100644 --- a/src/video_v4l2.cpp +++ b/src/video_v4l2.cpp @@ -555,10 +555,7 @@ static int v4l2_set_palette(ctx_v4l2cam *v4l2cam) if ((indx_palette >= 0) && (indx_palette <= V4L2_PALETTE_COUNT_MAX)) { retcd = v4l2_set_pixfmt(v4l2cam, palette_array[indx_palette].v4l2id); if (retcd >= 0) { - if (palette_array != NULL) { - free(palette_array); - palette_array = NULL; - } + myfree(&palette_array); return 0; } MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO @@ -602,10 +599,7 @@ static int v4l2_set_palette(ctx_v4l2cam *v4l2cam) MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO ,_("Selected palette %s") ,palette_array[indx_palette].fourcc); - if (palette_array != NULL) { - free(palette_array); - palette_array = NULL; - } + myfree(&palette_array); return 0; } MOTION_LOG(ERR, TYPE_VIDEO, NO_ERRNO @@ -615,10 +609,7 @@ static int v4l2_set_palette(ctx_v4l2cam *v4l2cam) MOTION_LOG(ERR, TYPE_VIDEO, NO_ERRNO ,_("Unable to find a compatible palette format.")); - if (palette_array != NULL) { - free(palette_array); - palette_array = NULL; - } + myfree(&palette_array); return -1; @@ -677,10 +668,7 @@ static int v4l2_set_mmap(ctx_v4l2cam *v4l2cam) MOTION_LOG(ERR, TYPE_VIDEO, SHOW_ERRNO ,_("Error querying buffer %i\nVIDIOC_QUERYBUF: ") ,buffer_index); - if (v4l2cam->buffers != NULL) { - free(v4l2cam->buffers); - v4l2cam->buffers = NULL; - } + myfree(&v4l2cam->buffers); return -1; } @@ -691,10 +679,7 @@ static int v4l2_set_mmap(ctx_v4l2cam *v4l2cam) if (v4l2cam->buffers[buffer_index].ptr == MAP_FAILED) { MOTION_LOG(ERR, TYPE_VIDEO, SHOW_ERRNO ,_("Error mapping buffer %i mmap"), buffer_index); - if (v4l2cam->buffers != NULL) { - free(v4l2cam->buffers); - v4l2cam->buffers = NULL; - } + myfree(&v4l2cam->buffers); return -1; } @@ -1103,10 +1088,7 @@ static void v4l2_log_formats(ctx_v4l2cam *v4l2cam) dev_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; } - if (palette_array != NULL) { - free(palette_array); - palette_array = NULL; - } + myfree(&palette_array); return; } @@ -1168,42 +1150,22 @@ void v4l2_cleanup(ctx_cam *cam) for (indx = 0; indx < (int)cam->v4l2cam->req.count; indx++){ munmap(cam->v4l2cam->buffers[indx].ptr, cam->v4l2cam->buffers[indx].size); } - if (cam->v4l2cam->buffers != NULL) { - free(cam->v4l2cam->buffers); - cam->v4l2cam->buffers = NULL; - } + myfree(&cam->v4l2cam->buffers); } if (cam->v4l2cam->devctrl_count != 0) { for (indx = 0; indx < cam->v4l2cam->devctrl_count; indx++){ - if (cam->v4l2cam->devctrl_array[indx].ctrl_iddesc != NULL) { - free(cam->v4l2cam->devctrl_array[indx].ctrl_iddesc); - cam->v4l2cam->devctrl_array[indx].ctrl_iddesc = NULL; - } - - if (cam->v4l2cam->devctrl_array[indx].ctrl_name != NULL) { - free(cam->v4l2cam->devctrl_array[indx].ctrl_name); - cam->v4l2cam->devctrl_array[indx].ctrl_name = NULL; - } - } - if (cam->v4l2cam->devctrl_array != NULL) { - free(cam->v4l2cam->devctrl_array); - cam->v4l2cam->devctrl_array = NULL; + myfree(&cam->v4l2cam->devctrl_array[indx].ctrl_iddesc); + myfree(&cam->v4l2cam->devctrl_array[indx].ctrl_name); } + myfree(&cam->v4l2cam->devctrl_array); } cam->v4l2cam->devctrl_count=0; util_parms_free(cam->v4l2cam->params); + myfree(&cam->v4l2cam->params); - if (cam->v4l2cam->params != NULL) { - free(cam->v4l2cam->params); - cam->v4l2cam->params = NULL; - } - - if (cam->v4l2cam != NULL) { - free(cam->v4l2cam); - cam->v4l2cam = NULL; - } + myfree(&cam->v4l2cam); cam->running_cam = false; #else diff --git a/src/webu.cpp b/src/webu.cpp index 2bd583a5..d707462f 100644 --- a/src/webu.cpp +++ b/src/webu.cpp @@ -109,45 +109,17 @@ static void webu_context_free(struct ctx_webui *webui) { int indx; - if (webui->auth_user != NULL) { - free(webui->auth_user); - webui->auth_user = NULL; - } - - if (webui->auth_pass != NULL) { - free(webui->auth_pass); - webui->auth_pass = NULL; - } - - if (webui->auth_opaque != NULL) { - free(webui->auth_opaque); - webui->auth_opaque = NULL; - } - - if (webui->auth_realm != NULL) { - free(webui->auth_realm); - webui->auth_realm = NULL; - } - - if (webui->resp_image != NULL) { - free(webui->resp_image); - webui->resp_image = NULL; - } + myfree(&webui->auth_user); + myfree(&webui->auth_pass); + myfree(&webui->auth_opaque); + myfree(&webui->auth_realm); + myfree(&webui->resp_image); for (indx = 0; indxpost_sz; indx++) { - if (webui->post_info[indx].key_nm != NULL) { - free(webui->post_info[indx].key_nm); - webui->post_info[indx].key_nm = NULL; - } - if (webui->post_info[indx].key_val != NULL) { - free(webui->post_info[indx].key_val); - webui->post_info[indx].key_val = NULL; - } - } - if (webui->post_info != NULL) { - free(webui->post_info); - webui->post_info = NULL; + myfree(&webui->post_info[indx].key_nm); + myfree(&webui->post_info[indx].key_val); } + myfree(&webui->post_info); delete webui; @@ -522,16 +494,10 @@ static mhdrslt webu_mhd_digest(struct ctx_webui *webui) /* Check for valid user name */ if (mystrne(user, webui->auth_user)) { webu_failauth_log(webui); - if (user != NULL) { - free(user); - user = NULL; - } + myfree(&user); return webu_mhd_digest_fail(webui, MHD_NO); } - if (user != NULL) { - free(user); - user = NULL; - } + myfree(&user); /* Check the password as well*/ retcd = MHD_digest_auth_check(webui->connection, webui->auth_realm @@ -590,38 +556,20 @@ static mhdrslt webu_mhd_basic(struct ctx_webui *webui) user = MHD_basic_auth_get_username_password (webui->connection, &pass); if ((user == NULL) || (pass == NULL)) { - if (user != NULL) { - free(user); - user = NULL; - } - if (pass != NULL) { - free(pass); - pass = NULL; - } + myfree(&user); + myfree(&pass); return webu_mhd_basic_fail(webui); } if ((mystrne(user, webui->auth_user)) || (mystrne(pass, webui->auth_pass))) { webu_failauth_log(webui); - if (user != NULL) { - free(user); - user = NULL; - } - if (pass != NULL) { - free(pass); - pass = NULL; - } + myfree(&user); + myfree(&pass); return webu_mhd_basic_fail(webui); } - if (user != NULL) { - free(user); - user = NULL; - } - if (pass != NULL) { - free(pass); - pass = NULL; - } + myfree(&user); + myfree(&pass); webui->authenticated = true; @@ -635,14 +583,8 @@ static void webu_mhd_auth_parse(struct ctx_webui *webui) int auth_len; char *col_pos; - if (webui->auth_user != NULL) { - free(webui->auth_user); - webui->auth_user = NULL; - } - if (webui->auth_pass != NULL) { - free(webui->auth_pass); - webui->auth_pass = NULL; - } + myfree(&webui->auth_user); + myfree(&webui->auth_pass); auth_len = webui->motapp->cam_list[0]->conf->webcontrol_authentication.length(); col_pos =(char*) strstr(webui->motapp->cam_list[0]->conf->webcontrol_authentication.c_str() ,":"); @@ -1458,16 +1400,10 @@ void webu_deinit(struct ctx_motapp *motapp) } util_parms_free(motapp->webcontrol_headers); - if (motapp->webcontrol_headers != NULL) { - free(motapp->webcontrol_headers); - motapp->webcontrol_headers = NULL; - } + myfree(&motapp->webcontrol_headers); util_parms_free(motapp->webcontrol_actions); - if (motapp->webcontrol_actions != NULL) { - free(motapp->webcontrol_actions); - motapp->webcontrol_actions = NULL; - } + myfree(&motapp->webcontrol_actions); } diff --git a/src/webu_stream.cpp b/src/webu_stream.cpp index 4aa34148..05557fb0 100644 --- a/src/webu_stream.cpp +++ b/src/webu_stream.cpp @@ -32,10 +32,7 @@ static void webu_stream_mjpeg_checkbuffers(struct ctx_webui *webui) { if (webui->resp_size < (size_t)webui->cam->imgs.size_norm) { if (webui->resp_image != NULL) { - if (webui->resp_image != NULL) { - free(webui->resp_image); - webui->resp_image = NULL; - } + myfree(&webui->resp_image); } webui->resp_image =(char*) mymalloc(webui->cam->imgs.size_norm); memset(webui->resp_image,'\0',webui->cam->imgs.size_norm); @@ -494,35 +491,12 @@ void webu_stream_deinit(struct ctx_cam *cam) pthread_mutex_destroy(&cam->stream.mutex); - if (cam->imgs.image_substream != NULL) { - free(cam->imgs.image_substream); - cam->imgs.image_substream = NULL; - } - - if (cam->stream.norm.jpeg_data != NULL) { - free(cam->stream.norm.jpeg_data); - cam->stream.norm.jpeg_data = NULL; - } - - if (cam->stream.sub.jpeg_data != NULL) { - free(cam->stream.sub.jpeg_data); - cam->stream.sub.jpeg_data = NULL; - } - - if (cam->stream.motion.jpeg_data != NULL) { - free(cam->stream.motion.jpeg_data); - cam->stream.motion.jpeg_data = NULL; - } - - if (cam->stream.source.jpeg_data != NULL) { - free(cam->stream.source.jpeg_data); - cam->stream.source.jpeg_data = NULL; - } - - if (cam->stream.secondary.jpeg_data != NULL) { - free(cam->stream.secondary.jpeg_data); - cam->stream.secondary.jpeg_data = NULL; - } + myfree(&cam->imgs.image_substream); + myfree(&cam->stream.norm.jpeg_data); + myfree(&cam->stream.sub.jpeg_data); + myfree(&cam->stream.motion.jpeg_data); + myfree(&cam->stream.source.jpeg_data); + myfree(&cam->stream.secondary.jpeg_data); } @@ -647,10 +621,7 @@ static void webu_stream_getimg_secondary(struct ctx_cam *cam) cam->stream.secondary.jpeg_size = cam->imgs.size_secondary; pthread_mutex_unlock(&cam->algsec->mutex); } else { - if (cam->stream.secondary.jpeg_data != NULL) { - free(cam->stream.secondary.jpeg_data); - cam->stream.secondary.jpeg_data = NULL; - } + myfree(&cam->stream.secondary.jpeg_data); } }