mirror of
https://github.com/pybind/pybind11.git
synced 2025-02-22 16:39:29 +00:00
Update memory leak test to use local dict
Use a local dictionary instead of py::globals(). This should still cause the test to fail.
This commit is contained in:
parent
d27b39e15d
commit
4c3d1c8d67
@ -309,29 +309,18 @@ PYBIND11_EMBEDDED_MODULE(test_memory_leak, m) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Test that C++ objects are properly deconstructed.") {
|
TEST_CASE("Test that C++ objects are properly deconstructed.") {
|
||||||
|
{
|
||||||
|
auto local = py::dict();
|
||||||
|
local['__builtins__'] = py::globals()['__builtins__'];
|
||||||
py::exec(R"(
|
py::exec(R"(
|
||||||
from test_memory_leak import TestObject
|
from test_memory_leak import TestObject
|
||||||
|
|
||||||
guard = TestObject(lambda: None)
|
guard = TestObject(lambda: None)
|
||||||
)", py::globals(), py::globals());
|
)", local, local);
|
||||||
REQUIRE(1 == TestObject::_count);
|
REQUIRE(1 == TestObject::_count);
|
||||||
// Explicitly remove reference to the object from the global dictionary. This
|
}
|
||||||
// allows the object to be garbage collected
|
|
||||||
py::globals()["guard"] = py::none();
|
|
||||||
|
|
||||||
py::finalize_interpreter();
|
py::finalize_interpreter();
|
||||||
py::initialize_interpreter();
|
|
||||||
|
|
||||||
REQUIRE(0 == TestObject::_count);
|
REQUIRE(0 == TestObject::_count);
|
||||||
py::exec(R"(
|
|
||||||
from test_memory_leak import TestObject
|
|
||||||
|
|
||||||
guard = TestObject(lambda: None)
|
|
||||||
)", py::globals(), py::globals());
|
|
||||||
REQUIRE(1 == TestObject::_count);
|
|
||||||
|
|
||||||
py::finalize_interpreter();
|
|
||||||
py::initialize_interpreter();
|
py::initialize_interpreter();
|
||||||
|
|
||||||
REQUIRE(0 == TestObject::_count);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user