Skip to content

Commit

Permalink
[pydrake] Add regression test for a memory leak (#22517)
Browse files Browse the repository at this point in the history
The leak is not yet fixed, so the test allows the leak, and uses the
leaks_required parameter as a change detector.
  • Loading branch information
rpoyner-tri authored Jan 23, 2025
1 parent 62b40b0 commit 60574f2
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion bindings/pydrake/test/memory_leak_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from pydrake.planning import RobotDiagramBuilder
from pydrake.systems.analysis import Simulator
from pydrake.systems.framework import DiagramBuilder
from pydrake.systems.framework import DiagramBuilder, LeafSystem
from pydrake.systems.primitives import ConstantVectorSource

from pydrake.common import RandomGenerator
Expand Down Expand Up @@ -269,6 +269,18 @@ def _dut_full_example():
return _make_sentinels_from_locals("full_example", locals())


class MyPyLeafSystem(LeafSystem):
def __init__(self):
super().__init__() # Don't forget to initialize the base class.
# Comment/uncomment this one line to turn leak off or on.
self._a_port = self.DeclareVectorInputPort(name="a", size=2)


def _dut_mypyleafsystem():
mypyleafsystem = MyPyLeafSystem()
return _make_sentinels_from_locals("mypyleafsystem", locals())


def _repeat(*, dut: callable, count: int):
"""Calls dut() for count times in a row, performing a full garbage
collection before and after each call. Tracks memory leaks of interest; the
Expand Down Expand Up @@ -324,3 +336,8 @@ def test_mixed_language_simulator(self):
def test_full_example(self):
# Note: this test doesn't invoke the #14355 deliberate cycle.
self.do_test(dut=_dut_full_example, count=1)

def test_mypyleafsystem(self):
# TODO(#22515): Fix this family of leaks.
self.do_test(dut=_dut_mypyleafsystem, count=1, leaks_allowed=1,
leaks_required=1)

0 comments on commit 60574f2

Please sign in to comment.