Skip to content

Commit de51a87

Browse files
committed
test_mem_fun: Test auto-disconnection with trackable.
This is probably tested somewhere else already, but I like having it here too because it is an important reason for slot<> to exist, compared to a simple std::function.
1 parent 1675c09 commit de51a87

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/test_mem_fun.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,34 @@ void test_bound()
142142
#endif
143143
}
144144

145+
class TestAutoDisconnect : public sigc::trackable
146+
{
147+
public:
148+
void foo()
149+
{
150+
result_stream << "TestAutoDisconnect::foo() called.";
151+
}
152+
};
153+
154+
void test_auto_disconnect()
155+
{
156+
//Check that slot doesn't try to call a method on a destroyed instance,
157+
//when the instance's class derives from trackable.
158+
sigc::slot<void()> slot_of_member_method;
159+
{
160+
TestAutoDisconnect t;
161+
slot_of_member_method = sigc::mem_fun(t, &TestAutoDisconnect::foo);
162+
163+
//The method should be called:
164+
slot_of_member_method();
165+
util->check_result(result_stream, "TestAutoDisconnect::foo() called.");
166+
}
167+
168+
//The method should not be called:
169+
slot_of_member_method();
170+
util->check_result(result_stream, "");
171+
}
172+
145173
int
146174
main(int argc, char* argv[])
147175
{
@@ -163,5 +191,7 @@ main(int argc, char* argv[])
163191

164192
test_bound();
165193

194+
test_auto_disconnect();
195+
166196
return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
167197
}

0 commit comments

Comments
 (0)