From 086de7dcacca56007c761c2bc5f18d6b2a4e200d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Sat, 3 Sep 2022 02:43:51 +0200 Subject: [PATCH] spa: libcamera: properly dispose of shared_ptr Previously, in `remove_device()`, the last device would be copied into the slot of the to-be-remove device. The problem with this is that it left the shared_ptr untouched in the previously last slot, and hence creating an extra reference. Fix this by moving instead of copying. A similar problem is present in `clear_devices()` which also did not properly dispose of the shared_ptrs. Fix that by calling `reset()` on each device's camera pointer. --- spa/plugins/libcamera/libcamera-manager.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spa/plugins/libcamera/libcamera-manager.cpp b/spa/plugins/libcamera/libcamera-manager.cpp index ed48c2cc2..e4ce49c63 100644 --- a/spa/plugins/libcamera/libcamera-manager.cpp +++ b/spa/plugins/libcamera/libcamera-manager.cpp @@ -143,12 +143,14 @@ static struct device *find_device(struct impl *impl, const Camera *camera) static void remove_device(struct impl *impl, struct device *device) { - *device = impl->devices[--impl->n_devices]; + device->camera.reset(); + *device = std::move(impl->devices[--impl->n_devices]); } static void clear_devices(struct impl *impl) { - impl->n_devices = 0; + while (impl->n_devices > 0) + impl->devices[--impl->n_devices].camera.reset(); } static int emit_object_info(struct impl *impl, struct device *device)