Skip to content

Commit 299e339

Browse files
committed
slot_base::set_parent(): Create a dummy slot_rep if necessary
set_parent() must always store the supplied parent pointer and cleanup function pointer, or else there may be a memory leak. The pointers are stored in slot_rep. Bug 167714
1 parent 01925e3 commit 299e339

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

sigc++/functors/slot_base.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@
2020
#include <sigc++/functors/slot_base.h>
2121
#include <sigc++/weak_raw_ptr.h>
2222

23+
namespace
24+
{
25+
// Used by slot_base::set_parent() when a slot_base without a rep_ is assigned a parent.
26+
class dummy_slot_rep : public sigc::internal::slot_rep
27+
{
28+
public:
29+
dummy_slot_rep() : slot_rep(nullptr) {}
30+
sigc::internal::slot_rep* clone() const override { return new dummy_slot_rep(); }
31+
void destroy() override {}
32+
};
33+
} // anonymous namespace
34+
2335
namespace sigc
2436
{
2537
namespace internal
@@ -243,8 +255,9 @@ slot_base::operator=(slot_base&& src)
243255
void
244256
slot_base::set_parent(notifiable* parent, notifiable::func_destroy_notify cleanup) const noexcept
245257
{
246-
if (rep_)
247-
rep_->set_parent(parent, cleanup);
258+
if (!rep_)
259+
rep_ = new dummy_slot_rep();
260+
rep_->set_parent(parent, cleanup);
248261
}
249262

250263
void

0 commit comments

Comments
 (0)