From 7e133d3be2437311a19865701c944449ed3e6b12 Mon Sep 17 00:00:00 2001 From: Evgeny Gagua Date: Thu, 12 Feb 2026 04:00:57 +0200 Subject: [PATCH] Fixed some memory issues - must be checked with Valgrind --- lib/App/common/SQLite_dataBinder.cpp | 1 + lib/App/common/hardwareManager.cpp | 3 + lib/App/common/kernelEventFilterBubble.cpp | 10 +-- lib/App/common/kernelModuleEmbedVideo.cpp | 12 ++-- lib/App/mojo/mojoKernel.cpp | 5 +- lib/components/base/basicPosition.cpp | 4 ++ lib/data/media/video/videoAVLIBSource.cpp | 79 +++++++++++----------- 7 files changed, 61 insertions(+), 53 deletions(-) diff --git a/lib/App/common/SQLite_dataBinder.cpp b/lib/App/common/SQLite_dataBinder.cpp index 39ed9d19..4b15cd01 100644 --- a/lib/App/common/SQLite_dataBinder.cpp +++ b/lib/App/common/SQLite_dataBinder.cpp @@ -2896,6 +2896,7 @@ DBC_RESOURCE_DATA *sqliteDBC::getResourceData( uint32_t ResourceID ){ /// @todo Valgrind tells us that tVal is lost and never freed tVal = (DBC_RESOURCE_DATA*) malloc(sizeof(DBC_RESOURCE_DATA)); if( tVal != NULL ) { + memset( tVal, 0, sizeof( DBC_RESOURCE_DATA )); Assoc::iterator it; cArr = result->at(0); tVal->id = ResourceID; diff --git a/lib/App/common/hardwareManager.cpp b/lib/App/common/hardwareManager.cpp index 86f6f7ba..026e2dbe 100644 --- a/lib/App/common/hardwareManager.cpp +++ b/lib/App/common/hardwareManager.cpp @@ -204,6 +204,8 @@ void hardwareManager::unloadResources( void ){ case HARDWARE_NIF_WIFI: case HARDWARE_NIF_VIRTUAL: case HARDWARE_NIF_TUNEL: delete( ( hardwareResourceNetwork*)(*idx) ); break; + case HARDWARE_ANALOG_VIDEO_IN: + case HARDWARE_DIGITAL_VIDEO_IN: delete( ( hardwareResourceVideoProvider*)(*idx) ); break; }; }; m_vResources.clear(); @@ -346,6 +348,7 @@ void hardwareManager::loadResources( void ){ } else { m_pDBC->setRessourceActive( *index, false ); }; + free( tData ); } else { LOG_ERROR("Can not fetch Resource Data from Database!"); }; diff --git a/lib/App/common/kernelEventFilterBubble.cpp b/lib/App/common/kernelEventFilterBubble.cpp index 3f2c86cf..78cd0cb4 100644 --- a/lib/App/common/kernelEventFilterBubble.cpp +++ b/lib/App/common/kernelEventFilterBubble.cpp @@ -190,8 +190,8 @@ bool kernelEventFilterBubble::updateBubble( TARGET_SEGMENT *bubble, TARGET_DATA if ( target == NULL ) return false; uint64_t cts = getMilliseconds(); bubble->ttl = cts + m_sBubbleFilterData.ttl; - //Only Active Bubles if( bubble->mode < TS_MODE_PERMANENT ){ + //Only Active Bubles if ( bubble->mode < TS_MODE_PUNCHED ) { #ifdef BUBBLE_TIME_BUCKET uint8_t bucket_id = (uint8_t) (( cts - bubble->bts ) / m_sBubbleFilterData.bucket_time ) & 0x1F ; @@ -204,17 +204,17 @@ bool kernelEventFilterBubble::updateBubble( TARGET_SEGMENT *bubble, TARGET_DATA #else bubble->hit_count++; #endif - if ( bubble->hit_count == m_sBubbleFilterData.t2t ) { - bubble->mode = TS_MODE_PUNCHED; //westlake 200 + if ( bubble->hit_count >= m_sBubbleFilterData.t2t ) { + bubble->mode = TS_MODE_PUNCHED; bubble->boundary = m_sBubbleFilterData.maximal_range; - }; + }; } else { // Generate signal to Kernel to continue session on TS_MODE_BLOCKED target // Triggering Session in Kernel kernelPrototype *AK = kernelPrototype::getInstance(); if ( AK ) AK->startSession(); }; - + //All Dynamic changes Just in Non Frozen Bubbles switch ( m_sBubbleFilterData.method ) { case TF_BUBBLE_METHOD_FIXED:{ break; diff --git a/lib/App/common/kernelModuleEmbedVideo.cpp b/lib/App/common/kernelModuleEmbedVideo.cpp index 14a2faea..40d2e949 100644 --- a/lib/App/common/kernelModuleEmbedVideo.cpp +++ b/lib/App/common/kernelModuleEmbedVideo.cpp @@ -3821,11 +3821,9 @@ void streamHandler::m_fThreadProc( void ){ }; }; }; +#endif //EVE_ENABLE_EASE_OUTPUT_FPS if ( nQueueSize ) { -#else //!EVE_ENABLE_EASE_OUTPUT_FPS - if ( nQueueSize ) { -#endif m_mEncodingQueueProtector.lock(); ENCODING_BUFFER *tData = m_qEncodingQueue.front(); if( tData ){ @@ -3856,14 +3854,12 @@ void streamHandler::m_fThreadProc( void ){ }; // Don't free tData->data since is pointer on static data free( tData->data ); - free( tData ); #else // !DIRECT_MJPEG_COMPOSING #ifdef OPENCV_VIDEO_OUTPUT cv::Mat image( tData->rows, tData->cols, CV_8UC3 ); memcpy( image.data, tData->data, tData->size ); free( tData->data ); - free( tData ); #ifdef EVE_ENABLE_EASE_OUTPUT_FPS if( !bSkeepA ) #endif @@ -3882,13 +3878,12 @@ void streamHandler::m_fThreadProc( void ){ m_nSegBFrameCounter++; m_mFrameCounterProtector.unlock(); }; - #else // !OPENCV_VIDEO_OUTPUT + #else //!OPENCV_VIDEO_OUTPUT //av_frame_make_writable( m_pAVFrame ); //m_qEncodingQueue.pop(); //m_mEncodingQueueProtector.unlock(); sws_scale( m_pConvertCtx, tData->data, tData->linesize, 0, tData->rows, m_pAVFrame->data, m_pAVFrame->linesize ); - free( tData->data[0] ); - free( tData ); + free( tData->data[0] ); /* @@ -3919,6 +3914,7 @@ void streamHandler::m_fThreadProc( void ){ #endif //!SEPARATED_VIDEO_ENCODERS #endif //!OPENCV_VIDEO_OUTPUT #endif //!DIRECT_MJPEG_COMPOSING + free( tData ); m_mFrameCounterProtector.lock(); m_nGlobalFrameCounter++; m_mFrameCounterProtector.unlock(); diff --git a/lib/App/mojo/mojoKernel.cpp b/lib/App/mojo/mojoKernel.cpp index 95af0706..98f5d87a 100644 --- a/lib/App/mojo/mojoKernel.cpp +++ b/lib/App/mojo/mojoKernel.cpp @@ -1374,7 +1374,10 @@ bool mojoKernel::validateReports( void ){ DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData(1); if( tSiteData ) { // Check if settings has been changed in DB - if ( tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty ) return false; + if ( tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty ) { + memcpy( &m_sSiteData, tSiteData, sizeof( DBC_SITE_DATA )); + free( tSiteData ); + }; } else { LOG_ERROR("Error Loading Site DATA!"); return false; diff --git a/lib/components/base/basicPosition.cpp b/lib/components/base/basicPosition.cpp index f4eb165a..9c6de51b 100644 --- a/lib/components/base/basicPosition.cpp +++ b/lib/components/base/basicPosition.cpp @@ -293,4 +293,8 @@ void basicPosition::convertLocalXYZ( C3D_DECART POS, C3D_SPH_RADIANS& LO */ // Destructor implementation basicPosition::~basicPosition(){ + if( m_pTransform != NULL ){ + delete( m_pTransform ); + m_pTransform = NULL; + }; }; diff --git a/lib/data/media/video/videoAVLIBSource.cpp b/lib/data/media/video/videoAVLIBSource.cpp index bc031957..66fdaa06 100644 --- a/lib/data/media/video/videoAVLIBSource.cpp +++ b/lib/data/media/video/videoAVLIBSource.cpp @@ -385,7 +385,11 @@ bool videoAVLIBSource::_open( char *URI, bool ShowDeviceMenu, float framerate, i char Buffer[1024] = {0}; char tChar[128] = {0}; strncpy( m_pVideoState->m_pFileName, URI, MAX_PATH_LEN ); - sprintf( m_pCookie, "%s", MD5String(m_pVideoState->m_pFileName) ); + CSTR tMD5 = MD5String(m_pVideoState->m_pFileName ); + if ( tMD5 ) { + sprintf( m_pCookie, "%s", tMD5 ); + free( tMD5 ); + } else sprintf( m_pCookie, "some-random-id" ); m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENING; if( strncmp( m_pVideoState->m_pFileName, "video=", 6 ) == 0 ){ @@ -788,44 +792,42 @@ void videoAVLIBSource::m_fThreadProc( void ){ */ m_bThreadStatus = TH_WORKING; - pPacket = (AVPacket *) av_malloc( sizeof(AVPacket) ); - if ( pPacket == NULL ) { + + AVPacket Packet; + av_init_packet( &Packet ); // Optional in newer API + Packet.data = NULL; + Packet.size = 0; + pFrame = av_frame_alloc(); + if ( pFrame == NULL ) { m_bThreadStatus = TH_ENDED; - LOG_ERROR("Error allocating AVPacket!"); + LOG_ERROR("Error allocating SRC AVFrame!"); } else { - av_init_packet( pPacket ); - pFrame = av_frame_alloc(); - if ( pFrame == NULL ) { + pFrameBGR = av_frame_alloc(); + if ( pFrameBGR == NULL ) { m_bThreadStatus = TH_ENDED; - LOG_ERROR("Error allocating SRC AVFrame!"); + LOG_ERROR("Error allocating BGR AVFrame!"); } else { - pFrameBGR = av_frame_alloc(); - if ( pFrameBGR == NULL ) { + dec_ret = av_image_alloc( pFrameBGR->data, pFrameBGR->linesize, m_nOutWidth, m_nOutHeight, AV_PIX_FMT_BGR24, BUFFER_ALIGN ); + sprintf( tB, "Allocated: %d bytes to Frame", dec_ret ); + LOG( tB ); + if ( dec_ret < 0 ) { m_bThreadStatus = TH_ENDED; - LOG_ERROR("Error allocating BGR AVFrame!"); + LOG_ERROR("Error allocating BGR AVFrame Buffer!"); } else { - dec_ret = av_image_alloc ( pFrameBGR->data, pFrameBGR->linesize, m_nOutWidth, m_nOutHeight, AV_PIX_FMT_BGR24, BUFFER_ALIGN ); - sprintf( tB, "Allocated: %d bytes to Frame", dec_ret ); - LOG( tB ); - if ( dec_ret < 0 ) { - m_bThreadStatus = TH_ENDED; - LOG_ERROR("Error allocating BGR AVFrame Buffer!"); - } else { - //pBGRbuffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN )); - //if ( pBGRbuffer == NULL ) { - // m_bThreadStatus = TH_ENDED; - // LOG_ERROR("Error allocating BGR AVFrame Buffer!"); - //} else { - // av_image_fill_arrays ( pFrameBGR->data, pFrameBGR->linesize, pBGRbuffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, BUFFER_ALIGN ); - // - // pFrameBGR->width = m_nWidth; - // pFrameBGR->height = m_nHeight; - // m_pVideoState->m_bStatus = MS_ALOCATED; + //pBGRbuffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN )); + //if ( pBGRbuffer == NULL ) { + // m_bThreadStatus = TH_ENDED; + // LOG_ERROR("Error allocating BGR AVFrame Buffer!"); + //} else { + // av_image_fill_arrays ( pFrameBGR->data, pFrameBGR->linesize, pBGRbuffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, BUFFER_ALIGN ); + // + // pFrameBGR->width = m_nWidth; + // pFrameBGR->height = m_nHeight; + // m_pVideoState->m_bStatus = MS_ALOCATED; #ifdef DEBUG - LOG("Video Reader Thread Started."); + LOG("Video Reader Thread Started."); #endif - }; }; }; }; @@ -847,11 +849,11 @@ void videoAVLIBSource::m_fThreadProc( void ){ #endif while( m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED ){ if ( m_pMediaDestination != NULL ){ - dec_ret = av_read_frame( m_pVideoState->m_pFormatCtx, pPacket ); + dec_ret = av_read_frame( m_pVideoState->m_pFormatCtx, &Packet ); if( dec_ret >= 0 ){ - if( pPacket->stream_index == m_pVideoState->m_iVideoStreamID ){ + if( Packet.stream_index == m_pVideoState->m_iVideoStreamID ){ m_pVideoState->m_bStatus = MS_PRESENT; - dec_ret = avcodec_send_packet( m_pVideoState->m_pCodecCtx, pPacket ); + dec_ret = avcodec_send_packet( m_pVideoState->m_pCodecCtx, &Packet ); if( dec_ret < 0 && dec_ret != AVERROR(EAGAIN) /* && dec_ret != AVERROR_EOF */ ){ LOG_ERROR( "Error sending video package to decoder" ); m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING; @@ -888,6 +890,7 @@ void videoAVLIBSource::m_fThreadProc( void ){ }; }; }; + av_packet_unref( &Packet ); } else if( dec_ret != AVERROR(EAGAIN) && dec_ret != AVERROR_EOF ){ LOG_ERROR( "Error reading video package" ); char tBuffer[1024]; @@ -907,10 +910,7 @@ void videoAVLIBSource::m_fThreadProc( void ){ m_bThreadStatus = TH_ENDED; }; }; - //m_pVideoState->m_bStatus = MS_STOPED; - SLEEP( 5 ); }; - av_packet_unref( pPacket ); SLEEP( 5 ); } else SLEEP( 5 ); }; @@ -920,10 +920,11 @@ void videoAVLIBSource::m_fThreadProc( void ){ if ( pFrame != NULL ) { av_frame_free( &pFrame ); }; - if ( pPacket != NULL ) { - av_free( pPacket ); + if ( pFrameBGR ) { + if( pFrameBGR->data[0] ) av_freep(&pFrameBGR->data[0]); + av_frame_free( &pFrameBGR ); }; - if( pBGRbuffer != NULL ) { + if (pBGRbuffer != NULL) { av_free( pBGRbuffer ); }; m_bThreadStatus = TH_ENDED;