Skip to content

Commit 88124ef

Browse files
committed
fix garbage collection issue
1 parent 64b9a4c commit 88124ef

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/idom/core/layout.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def _render_model_children(
255255
key = child.get("key") or hex(id(child))[2:]
256256
elif isinstance(child, AbstractComponent):
257257
child_type = COMPONENT_TYPE
258-
key = getattr(child, "key", "") or hex(id(child))[2:]
258+
key = getattr(child, "key", None) or hex(id(child))[2:]
259259
else:
260260
child = str(child)
261261
child_type = STRING_TYPE
@@ -288,6 +288,8 @@ def _render_model_children(
288288
elif child_type is COMPONENT_TYPE:
289289
old_child_state = old_state.children_by_key.get(key)
290290
if old_child_state is not None:
291+
old_component = old_child_state.life_cycle_hook.component
292+
del self._model_state_by_component_id[id(old_component)]
291293
new_child_state = old_child_state.new(new_state, child)
292294
else:
293295
hook = LifeCycleHook(child, self)
@@ -329,10 +331,6 @@ def _unmount_model_states(self, old_states: List[_ModelState]) -> None:
329331
hook = state.life_cycle_hook
330332
hook.component_will_unmount()
331333
del self._model_state_by_component_id[id(hook.component)]
332-
import gc
333-
334-
print(state)
335-
print(gc.get_referrers(hook))
336334
to_unmount.extend(state.children_by_key.values())
337335

338336
def __repr__(self) -> str:

tests/test_core/test_layout.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ def Inner():
387387
async def test_hooks_for_keyed_components_get_garbage_collected():
388388
pop_item = idom.Ref(None)
389389
garbage_collect_items = []
390+
registered_finalizers = set()
390391

391392
@idom.component
392393
def Outer():
@@ -396,7 +397,10 @@ def Outer():
396397

397398
@idom.component
398399
def Inner(key):
399-
finalize(idom.hooks.current_hook(), lambda: garbage_collect_items.append(key))
400+
if key not in registered_finalizers:
401+
hook = idom.hooks.current_hook()
402+
finalize(hook, lambda: garbage_collect_items.append(key))
403+
registered_finalizers.add(key)
400404
return idom.html.div(key)
401405

402406
async with idom.Layout(Outer()) as layout:

0 commit comments

Comments
 (0)