hw/loongarch/virt: free flash devices and OEM strings on finalization
Flash devices created in virt_initfn() via qdev_new() hold an extra reference that is only dropped on sysbus_realize_and_unref(). When the machine is destroyed before realization, the flash objects leak. Also, the oem_id and oem_table_id strings from g_strndup() are never freed. Fixes: 445c9c645 ("hw/loongarch/virt: Allow user to customize OEM ID and OEM table ID") Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Marc-André Lureau committed
Apr 27, 2026 at 16:21 UTC
b54224824315c3b1f52e89ace36b4f0df5cde05a
1 file changed
+14
hw/loongarch/virt.c
+14
@@ -1566,6 +1566,19 @@ static void virt_class_init(ObjectClass *oc, const void *data)
1566
#define DEFINE_VIRT_MACHINE(major, minor) \
1567
DEFINE_VIRT_MACHINE_VERSION(false, major, minor)
1568
1569
+static void virt_instance_finalize(Object *obj)
1570
+{
1571
+ LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(obj);
1572
+
1573
+ for (int i = 0; i < ARRAY_SIZE(lvms->flash); i++) {
1574
+ if (lvms->flash[i] && !qdev_is_realized(DEVICE(lvms->flash[i]))) {
1575
+ object_unref(OBJECT(lvms->flash[i]));
1576
+ }
1577
+ }
1578
+ g_free(lvms->oem_id);
1579
+ g_free(lvms->oem_table_id);
1580
+}
1581
+
1582
static const TypeInfo virt_machine_info = {
1583
.name = TYPE_LOONGARCH_VIRT_MACHINE,
1584
.parent = TYPE_MACHINE,
@@ -1573,6 +1586,7 @@ static const TypeInfo virt_machine_info = {
1586
.instance_size = sizeof(LoongArchVirtMachineState),
1587
.class_init = virt_class_init,
1588
.instance_init = virt_initfn,
1589
+ .instance_finalize = virt_instance_finalize,
1590
.interfaces = (InterfaceInfo[]) {
1591
{ TYPE_HOTPLUG_HANDLER },
1592
{ }