From 98bd9cb8c9f370c977dc3b6cc14f3d1ebfd24563 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Fri, 8 Feb 2019 00:24:14 +0200 Subject: libcamera: signal: Disconnect signal automatically on slot deletion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a signal is connected to a member function slot, the slot is not disconnected when the slot object is deleted. This can lead to calling a member function of a deleted object if the signal isn't disconnected manually by the slot object's destructor. Make signal handling easier by implementing a base Object class that tracks all connected signals and disconnects from them automatically when the object is deleted, using template specialization resolution in the Signal class. As inheriting from the Object class may to a too harsh requirement for Signal usage in applications, keep the existing behaviour working if the slot doesn't inherit from the Object class. We may reconsider this later and require all slot objects to inherit from the Object class. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- include/libcamera/meson.build | 1 + include/libcamera/object.h | 35 +++++++++++++ include/libcamera/signal.h | 111 +++++++++++++++++++++++++++++------------- 3 files changed, 113 insertions(+), 34 deletions(-) create mode 100644 include/libcamera/object.h (limited to 'include') diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 5788e9bb..3f4d1e28 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -5,6 +5,7 @@ libcamera_api = files([ 'event_dispatcher.h', 'event_notifier.h', 'libcamera.h', + 'object.h', 'request.h', 'signal.h', 'stream.h', diff --git a/include/libcamera/object.h b/include/libcamera/object.h new file mode 100644 index 00000000..eadd41f9 --- /dev/null +++ b/include/libcamera/object.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * object.h - Base object + */ +#ifndef __LIBCAMERA_OBJECT_H__ +#define __LIBCAMERA_OBJECT_H__ + +#include + +namespace libcamera { + +class SignalBase; +template +class Signal; + +class Object +{ +public: + virtual ~Object(); + +private: + template + friend class Signal; + + void connect(SignalBase *signal); + void disconnect(SignalBase *signal); + + std::list signals_; +}; + +}; /* namespace libcamera */ + +#endif /* __LIBCAMERA_OBJECT_H__ */ diff --git a/include/libcamera/signal.h b/include/libcamera/signal.h index 6e8b876e..c8f3243e 100644 --- a/include/libcamera/signal.h +++ b/include/libcamera/signal.h @@ -10,32 +10,47 @@ #include #include +#include + namespace libcamera { template class Signal; -template class SlotBase { public: - SlotBase(void *obj) - : obj_(obj) {} + SlotBase(void *obj, bool isObject) + : obj_(obj), isObject_(isObject) {} virtual ~SlotBase() {} + void *obj() { return obj_; } + bool isObject() const { return isObject_; } + +protected: + void *obj_; + bool isObject_; +}; + +template +class SlotArgs : public SlotBase +{ +public: + SlotArgs(void *obj, bool isObject) + : SlotBase(obj, isObject) {} + virtual void invoke(Args... args) = 0; protected: friend class Signal; - void *obj_; }; template -class SlotMember : public SlotBase +class SlotMember : public SlotArgs { public: - SlotMember(T *obj, void (T::*func)(Args...)) - : SlotBase(obj), func_(func) {} + SlotMember(T *obj, bool isObject, void (T::*func)(Args...)) + : SlotArgs(obj, isObject), func_(func) {} void invoke(Args... args) { (static_cast(this->obj_)->*func_)(args...); } @@ -45,11 +60,11 @@ private: }; template -class SlotStatic : public SlotBase +class SlotStatic : public SlotArgs { public: SlotStatic(void (*func)(Args...)) - : SlotBase(nullptr), func_(func) {} + : SlotArgs(nullptr, false), func_(func) {} void invoke(Args... args) { (*func_)(args...); } @@ -58,21 +73,57 @@ private: void (*func_)(Args...); }; +class SignalBase +{ +public: + template + void disconnect(T *object) + { + for (auto iter = slots_.begin(); iter != slots_.end(); ) { + SlotBase *slot = *iter; + if (slot->obj() == object) { + iter = slots_.erase(iter); + delete slot; + } else { + ++iter; + } + } + } + +protected: + friend class Object; + std::list slots_; +}; + template -class Signal +class Signal : public SignalBase { public: Signal() {} ~Signal() { - for (SlotBase *slot : slots_) + for (SlotBase *slot : slots_) { + if (slot->isObject()) + static_cast(slot->obj())->disconnect(this); delete slot; + } + } + +#ifndef __DOXYGEN__ + template::value>::type * = nullptr> + void connect(T *object, void (T::*func)(Args...)) + { + object->connect(this); + slots_.push_back(new SlotMember(object, true, func)); } + template::value>::type * = nullptr> +#else template +#endif void connect(T *object, void (T::*func)(Args...)) { - slots_.push_back(new SlotMember(object, func)); + slots_.push_back(new SlotMember(object, false, func)); } void connect(void (*func)(Args...)) @@ -82,7 +133,7 @@ public: void disconnect() { - for (SlotBase *slot : slots_) + for (SlotBase *slot : slots_) delete slot; slots_.clear(); } @@ -90,27 +141,21 @@ public: template void disconnect(T *object) { - for (auto iter = slots_.begin(); iter != slots_.end(); ) { - SlotBase *slot = *iter; - if (slot->obj_ == object) { - iter = slots_.erase(iter); - delete slot; - } else { - ++iter; - } - } + SignalBase::disconnect(object); } template void disconnect(T *object, void (T::*func)(Args...)) { for (auto iter = slots_.begin(); iter != slots_.end(); ) { - SlotBase *slot = *iter; + SlotArgs *slot = static_cast *>(*iter); /* - * If the obj_ pointer matches the object types must - * match, so we can safely cast to SlotMember. + * If the obj() pointer matches the object, the slot is + * guaranteed to be a member slot, so we can safely + * cast it to SlotMember and access its + * func_ member. */ - if (slot->obj_ == object && + if (slot->obj() == object && static_cast *>(slot)->func_ == func) { iter = slots_.erase(iter); delete slot; @@ -123,8 +168,8 @@ public: void disconnect(void (*func)(Args...)) { for (auto iter = slots_.begin(); iter != slots_.end(); ) { - SlotBase *slot = *iter; - if (slot->obj_ == nullptr && + SlotArgs *slot = *iter; + if (slot->obj() == nullptr && static_cast *>(slot)->func_ == func) { iter = slots_.erase(iter); delete slot; @@ -140,13 +185,11 @@ public: * Make a copy of the slots list as the slot could call the * disconnect operation, invalidating the iterator. */ - std::vector *> slots{ slots_.begin(), slots_.end() }; - for (SlotBase *slot : slots) - slot->invoke(args...); + std::vector slots{ slots_.begin(), slots_.end() }; + for (SlotBase *slot : slots) { + static_cast *>(slot)->invoke(args...); + } } - -private: - std::list *> slots_; }; } /* namespace libcamera */ -- cgit v1.2.1