[Concept,03/35] test: dm: Fix memory leaks in test drivers

Message ID 20251210000737.180797-4-sjg@u-boot.org
State New
Headers
Series malloc: Add heap debugging commands and mcheck caller tracking |

Commit Message

Simon Glass Dec. 10, 2025, 12:06 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

The test_manual_drv driver allocates private data in its probe function
with calloc(), but never frees it in remove(). Add the missing free()
call to test_manual_remove().

Similarly, create_children() allocates platform data with calloc() and
sets it with dev_set_plat(), but doesn't set the DM_FLAG_ALLOC_PDATA
flag. This flag tells the device removal code to free the platform data.
Set this flag so driver model will free the allocated memory on unbind.

These fixes eliminate memory leaks that caused the malloc_dump output
to grow excessively after running DM tests.

Co-developed-by: Claude Opus 4 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 test/dm/core.c        | 1 +
 test/dm/test-driver.c | 3 +++
 2 files changed, 4 insertions(+)
  

Patch

diff --git a/test/dm/core.c b/test/dm/core.c
index 959b834576f..75c44e8328c 100644
--- a/test/dm/core.c
+++ b/test/dm/core.c
@@ -606,6 +606,7 @@  static int create_children(struct unit_test_state *uts, struct udevice *parent,
 		pdata = calloc(1, sizeof(*pdata));
 		pdata->ping_add = key + i;
 		dev_set_plat(dev, pdata);
+		dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
 		if (child)
 			child[i] = dev;
 	}
diff --git a/test/dm/test-driver.c b/test/dm/test-driver.c
index 759de3a5f77..fc4e3d82a1f 100644
--- a/test/dm/test-driver.c
+++ b/test/dm/test-driver.c
@@ -135,12 +135,15 @@  static int test_manual_probe(struct udevice *dev)
 static int test_manual_remove(struct udevice *dev)
 {
 	dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
+	free(dev_get_priv(dev));
+
 	return 0;
 }
 
 static int test_manual_unbind(struct udevice *dev)
 {
 	dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
+
 	return 0;
 }