From a0c51a1dfeec9cde2a0088cc65344d75753d9c31 Mon Sep 17 00:00:00 2001 From: Evgeni Raikhel Date: Mon, 17 Jun 2019 14:18:47 +0300 Subject: [PATCH] Fix memory and resource leak via user callback management Remove unused variables --- src/pipeline/resolver.h | 2 -- src/win7/win7-uvc.cpp | 16 ++++++++-------- src/win7/win7-uvc.h | 2 ++ src/win7/winusb_uvc/winusb_uvc.cpp | 1 - src/win7/winusb_uvc/winusb_uvc.h | 2 +- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/pipeline/resolver.h b/src/pipeline/resolver.h index 6fddf77d601..e3a781c037b 100644 --- a/src/pipeline/resolver.h +++ b/src/pipeline/resolver.h @@ -288,7 +288,6 @@ namespace librealsense // Unpack the data returned by assign std::map dev_to_profiles; - std::map stream_to_dev; std::map> stream_to_profile; std::map sensors_map; @@ -303,7 +302,6 @@ namespace librealsense for (auto && kvp : mapping) { dev_to_profiles[kvp.first].push_back(kvp.second); index_type idx{ kvp.second->get_stream_type(), kvp.second->get_stream_index() }; - stream_to_dev.emplace(idx, sensors_map.at(kvp.first)); stream_to_profile[idx] = kvp.second; } diff --git a/src/win7/win7-uvc.cpp b/src/win7/win7-uvc.cpp index a9442d2c220..f0ba0a5ac3c 100644 --- a/src/win7/win7-uvc.cpp +++ b/src/win7/win7-uvc.cpp @@ -610,14 +610,14 @@ namespace librealsense winusb_free_formats(formats); - callback_context *context = new callback_context(); - context->_callback = callback; - context->_this = this; - context->_profile = profile; - - auto sts = winusb_start_streaming(_device.get(), &ctrl, internal_winusb_uvc_callback, context, 0); - if(sts != UVC_SUCCESS) - throw std::runtime_error("Failed to start streaming!"); + _cb_context = std::make_shared(); + _cb_context->_callback = callback; + _cb_context->_profile = profile; + _cb_context->_this = this; + + auto sts = winusb_start_streaming(_device.get(), &ctrl, internal_winusb_uvc_callback, _cb_context.get(), 0); + if(sts != UVC_SUCCESS) + throw std::runtime_error("Failed to start streaming!"); } void win7_uvc_device::stream_on(std::function error_handler) diff --git a/src/win7/win7-uvc.h b/src/win7/win7-uvc.h index bafa79eb0da..09ca5cc90be 100644 --- a/src/win7/win7-uvc.h +++ b/src/win7/win7-uvc.h @@ -30,6 +30,7 @@ namespace librealsense namespace platform { class win7_backend; + struct callback_context; struct profile_and_callback { @@ -99,6 +100,7 @@ namespace librealsense std::mutex _streams_mutex; std::shared_ptr _backend; + std::shared_ptr _cb_context; std::string _location; usb_spec _device_usb_spec; diff --git a/src/win7/winusb_uvc/winusb_uvc.cpp b/src/win7/winusb_uvc/winusb_uvc.cpp index e23d9d78f6c..618b9722661 100644 --- a/src/win7/winusb_uvc/winusb_uvc.cpp +++ b/src/win7/winusb_uvc/winusb_uvc.cpp @@ -1115,7 +1115,6 @@ void winusb_uvc_stream_close(winusb_uvc_stream_handle_t *strmh) DL_DELETE(strmh->devh->streams, strmh); - free(strmh->user_ptr); free(strmh); } diff --git a/src/win7/winusb_uvc/winusb_uvc.h b/src/win7/winusb_uvc/winusb_uvc.h index dd9c12e3290..40d12be3e26 100644 --- a/src/win7/winusb_uvc/winusb_uvc.h +++ b/src/win7/winusb_uvc/winusb_uvc.h @@ -470,7 +470,7 @@ struct winusb_uvc_stream_handle { std::thread cb_thread; winusb_uvc_frame_callback_t *user_cb; - void *user_ptr; + void *user_ptr; // User-allocated memory enum uvc_frame_format frame_format; };