Skip to content

Commit 734a031

Browse files
yunowotbujewsk
andauthored
[DLStreamer] Fix memory leak in RegionOfInterest (open-edge-platform#1170)
Co-authored-by: Tomasz Bujewski <[email protected]>
1 parent 1ada981 commit 734a031

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

libraries/dl-streamer/include/dlstreamer/gst/videoanalytics/region_of_interest.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <gst/video/gstvideometa.h>
2222

2323
#include <cassert>
24+
#include <memory>
2425
#include <stdexcept>
2526
#include <string>
2627
#include <vector>
@@ -244,8 +245,11 @@ class RegionOfInterest {
244245
while (gst_analytics_relation_meta_get_direct_related(od_meta.meta, od_meta.id, GST_ANALYTICS_REL_TYPE_CONTAIN,
245246
GST_ANALYTICS_MTD_TYPE_ANY, &state, &handle)) {
246247
GstStructure *s = GVA::Tensor::convert_to_tensor(handle);
247-
if (s != nullptr)
248+
if (s != nullptr) {
249+
auto shared_s = std::shared_ptr<GstStructure>(s, gst_structure_free);
248250
_tensors.emplace_back(s);
251+
_converted_structures.push_back(shared_s);
252+
}
249253
}
250254
}
251255

@@ -371,6 +375,12 @@ class RegionOfInterest {
371375
* obtained from GstVideoRegionOfInterestMeta
372376
*/
373377
std::vector<Tensor> _tensors;
378+
379+
/**
380+
* @brief vector of GstStructure shared pointers that were allocated by convert_to_tensor.
381+
*/
382+
std::vector<std::shared_ptr<GstStructure>> _converted_structures;
383+
374384
/**
375385
* @brief last added detection Tensor instance, defined as Tensor with name set to "detection"
376386
*/

libraries/dl-streamer/include/dlstreamer/gst/videoanalytics/video_frame.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,21 +332,30 @@ class VideoFrame {
332332
}
333333

334334
std::vector<RegionOfInterest> get_regions() const {
335-
std::vector<RegionOfInterest> regions;
336-
gpointer state = NULL;
337-
GstAnalyticsRelationMeta *relation_meta;
338-
GstVideoRegionOfInterestMeta *roi_meta;
339-
GstAnalyticsODMtd od_mtd;
340-
341-
relation_meta = gst_buffer_get_analytics_relation_meta(buffer);
335+
GstAnalyticsRelationMeta *relation_meta = gst_buffer_get_analytics_relation_meta(buffer);
342336

343337
if (!relation_meta) {
344338
return {};
345339
}
346340

341+
// Count regions to pre-allocate vector capacity
342+
gpointer state = NULL;
343+
GstAnalyticsODMtd od_mtd;
344+
size_t count = 0;
345+
while (
346+
gst_analytics_relation_meta_iterate(relation_meta, &state, gst_analytics_od_mtd_get_mtd_type(), &od_mtd)) {
347+
++count;
348+
}
349+
350+
// Pre-allocate vector to avoid reallocation during emplace_back
351+
std::vector<RegionOfInterest> regions;
352+
regions.reserve(count);
353+
354+
// Construct RegionOfInterest objects
355+
state = NULL;
347356
while (
348357
gst_analytics_relation_meta_iterate(relation_meta, &state, gst_analytics_od_mtd_get_mtd_type(), &od_mtd)) {
349-
roi_meta = gst_buffer_get_video_region_of_interest_meta_id(buffer, od_mtd.id);
358+
GstVideoRegionOfInterestMeta *roi_meta = gst_buffer_get_video_region_of_interest_meta_id(buffer, od_mtd.id);
350359
if (!roi_meta) {
351360
throw std::runtime_error(
352361
"GVA::VideoFrame: Failed to get video region of interest meta for object detection metadata");

0 commit comments

Comments
 (0)