From 259b7f765526aac9551e4f80c96c61ff3bdc7802 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 31 Jul 2017 15:49:27 -0400 Subject: [PATCH] fix memleak --- src/zm_ffmpeg_camera.cpp | 22 ++++++++++------------ src/zm_packetqueue.cpp | 2 +- src/zm_videostore.cpp | 15 +++++++++++++-- src/zm_zone.cpp | 2 +- web/skins/classic/views/events.php | 4 +++- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index eaaf20456..dc29363cf 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -270,15 +270,15 @@ int FfmpegCamera::OpenFfmpeg() { } // Set transport method as specified by method field, rtpUni is default - if (Method() == "rtpMulti") { + if ( Method() == "rtpMulti" ) { ret = av_dict_set(&opts, "rtsp_transport", "udp_multicast", 0); - } else if (Method() == "rtpRtsp") { + } else if ( Method() == "rtpRtsp" ) { ret = av_dict_set(&opts, "rtsp_transport", "tcp", 0); - } else if (Method() == "rtpRtspHttp") { + } else if ( Method() == "rtpRtspHttp" ) { ret = av_dict_set(&opts, "rtsp_transport", "http", 0); } - if (ret < 0) { + if ( ret < 0 ) { Warning("Could not set rtsp_transport method '%s'\n", Method().c_str()); } @@ -297,7 +297,7 @@ int FfmpegCamera::OpenFfmpeg() { } AVDictionaryEntry *e; - if ((e = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX)) != NULL) { + if ( (e = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX)) != NULL ) { Warning( "Option %s not recognized by ffmpeg", e->key); } @@ -380,7 +380,7 @@ int FfmpegCamera::OpenFfmpeg() { mVideoCodecContext->flags2 |= CODEC_FLAG2_FAST | CODEC_FLAG_LOW_DELAY; // Try and get the codec from the codec context - if ((mVideoCodec = avcodec_find_decoder(mVideoCodecContext->codec_id)) == NULL) { + if ( (mVideoCodec = avcodec_find_decoder(mVideoCodecContext->codec_id)) == NULL ) { Fatal("Can't find codec for video stream from %s", mPath.c_str()); } else { Debug(1, "Video Found decoder"); @@ -388,10 +388,10 @@ int FfmpegCamera::OpenFfmpeg() { // Open the codec #if !LIBAVFORMAT_VERSION_CHECK(53, 8, 0, 8, 0) Debug ( 1, "Calling avcodec_open" ); - if (avcodec_open(mVideoCodecContext, mVideoCodec) < 0) + if ( avcodec_open(mVideoCodecContext, mVideoCodec) < 0 ) #else Debug ( 1, "Calling avcodec_open2" ); - if (avcodec_open2(mVideoCodecContext, mVideoCodec, 0) < 0) + if ( avcodec_open2(mVideoCodecContext, mVideoCodec, 0) < 0 ) #endif Fatal( "Unable to open codec for video stream from %s", mPath.c_str() ); } @@ -837,11 +837,9 @@ else if ( packet.pts && video_last_pts > packet.pts ) { Debug( 3, "Some other stream index %d", packet.stream_index ); #endif } - //if ( videoStore ) { - // the packet contents are ref counted... when queuing, we allocate another packet and reference it with that one, so we should always need to unref here, which should not affect the queued version. - zm_av_packet_unref( &packet ); - //} + // the packet contents are ref counted... when queuing, we allocate another packet and reference it with that one, so we should always need to unref here, which should not affect the queued version. + zm_av_packet_unref( &packet ); } // end while ! frameComplete return (frameCount); } // end FfmpegCamera::CaptureAndRecord diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index d4520caba..497809342 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -30,7 +30,7 @@ zm_packetqueue::zm_packetqueue(){ } zm_packetqueue::~zm_packetqueue() { - + clearQueue(); } bool zm_packetqueue::queuePacket( ZMPacket* zm_packet ) { diff --git a/src/zm_videostore.cpp b/src/zm_videostore.cpp index 9cfeb74a9..2fd710dd8 100644 --- a/src/zm_videostore.cpp +++ b/src/zm_videostore.cpp @@ -189,6 +189,8 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in, audio_output_codec = NULL; audio_input_context = NULL; audio_output_stream = NULL; + input_frame = NULL; + output_frame = NULL; #ifdef HAVE_LIBAVRESAMPLE resample_context = NULL; #endif @@ -391,6 +393,15 @@ Debug(2, "writing flushed packet pts(%d) dts(%d) duration(%d)", pkt.pts, pkt.dts /* free the stream */ avformat_free_context(oc); + + if ( input_frame ) { + av_frame_free( &input_frame ); + input_frame = NULL; + } + if ( output_frame ) { + av_frame_free( &output_frame ); + output_frame = NULL; + } } bool VideoStore::setup_resampler() { @@ -495,13 +506,13 @@ bool VideoStore::setup_resampler() { } /** Create a new frame to store the audio samples. */ - if (!(input_frame = zm_av_frame_alloc())) { + if ( !(input_frame = zm_av_frame_alloc()) ) { Error("Could not allocate input frame"); return false; } /** Create a new frame to store the audio samples. */ - if (!(output_frame = zm_av_frame_alloc())) { + if ( !(output_frame = zm_av_frame_alloc()) ) { Error("Could not allocate output frame"); av_frame_free( &input_frame ); return false; diff --git a/src/zm_zone.cpp b/src/zm_zone.cpp index 7e4d151ff..1b4cb946f 100644 --- a/src/zm_zone.cpp +++ b/src/zm_zone.cpp @@ -68,7 +68,7 @@ void Zone::Setup( overload_frames = p_overload_frames; extend_alarm_frames = p_extend_alarm_frames; - Debug( 1, "Initialised zone %d/%s - %d - %dx%d - Rgb:%06x, CM:%d, MnAT:%d, MxAT:%d, MnAP:%d, MxAP:%d, FB:%dx%d, MnFP:%d, MxFP:%d, MnBS:%d, MxBS:%d, MnB:%d, MxB:%d, OF: %d, AF: %d", id, label, type, polygon.Width(), polygon.Height(), alarm_rgb, check_method, min_pixel_threshold, max_pixel_threshold, min_alarm_pixels, max_alarm_pixels, filter_box.X(), filter_box.Y(), min_filter_pixels, max_filter_pixels, min_blob_pixels, max_blob_pixels, min_blobs, max_blobs, overload_frames, extend_alarm_frames ); + //Debug( 1, "Initialised zone %d/%s - %d - %dx%d - Rgb:%06x, CM:%d, MnAT:%d, MxAT:%d, MnAP:%d, MxAP:%d, FB:%dx%d, MnFP:%d, MxFP:%d, MnBS:%d, MxBS:%d, MnB:%d, MxB:%d, OF: %d, AF: %d", id, label, type, polygon.Width(), polygon.Height(), alarm_rgb, check_method, min_pixel_threshold, max_pixel_threshold, min_alarm_pixels, max_alarm_pixels, filter_box.X(), filter_box.Y(), min_filter_pixels, max_filter_pixels, min_blob_pixels, max_blob_pixels, min_blobs, max_blobs, overload_frames, extend_alarm_frames ); alarmed = false; was_alarmed = false; diff --git a/web/skins/classic/views/events.php b/web/skins/classic/views/events.php index 7aad2b36e..7c3ffbd4b 100644 --- a/web/skins/classic/views/events.php +++ b/web/skins/classic/views/events.php @@ -193,7 +193,9 @@ foreach ( $events as $event ) { Id().$filterQuery.$sortQuery.'&page=1', 'zmEvent', array( 'event', reScale( $event->Width(), $scale ), reScale( $event->Height(), $scale ) ), $event->Id().($event->Archived()?'*':'') ) ?> Id().$filterQuery.$sortQuery.'&page=1', 'zmEvent', array( 'event', reScale( $event->Width(), $event->DefaultScale(), ZM_WEB_DEFAULT_SCALE ), reScale( $event->Height(), $event->DefaultScale(), ZM_WEB_DEFAULT_SCALE ) ), validHtmlStr($event->Name()).($event->Archived()?'*':'' ) ) ?> - MonitorName() ?> + MonitorId(), 'zmMonitor'.$event->Monitorid(), 'monitor', $event->MonitorName(), canEdit( 'Monitors' ) ) + ?> Id(), 'zmEventDetail', 'eventdetail', validHtmlStr($event->Cause()), canEdit( 'Events' ), 'title="'.htmlspecialchars($event->Notes()).'"' ) ?> StartTime()) ) ?> Length() ) ?>