test: dm: Fix memory leaks in test drivers

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>
This commit is contained in:
Simon Glass
2025-12-08 16:28:17 -07:00
parent 5b1fec5f9b
commit e0aa4bfae6
2 changed files with 4 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -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;
}