Skip to content

Commit a0089c4

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 334079b commit a0089c4

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

sigc++/functors/slot_base.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ struct destroy_notify_struct
3636

3737
bool deleted_;
3838
};
39+
40+
// Used by slot_base::set_parent() when a slot_base without a rep_ is assigned a parent.
41+
class dummy_slot_rep : public sigc::internal::slot_rep
42+
{
43+
public:
44+
dummy_slot_rep() : slot_rep(nullptr, nullptr, &clone) {}
45+
static void* clone(void*) { return new dummy_slot_rep(); }
46+
};
3947
} // anonymous namespace
4048

4149
namespace sigc
@@ -263,8 +271,9 @@ slot_base& slot_base::operator=(slot_base&& src)
263271

264272
void slot_base::set_parent(void* parent, void* (*cleanup)(void*)) const noexcept
265273
{
266-
if (rep_)
267-
rep_->set_parent(parent, cleanup);
274+
if (!rep_)
275+
rep_ = new dummy_slot_rep();
276+
rep_->set_parent(parent, cleanup);
268277
}
269278

270279
void slot_base::add_destroy_notify_callback(void* data, func_destroy_notify func) const

0 commit comments

Comments
 (0)