Commit 0d6ee59450 for qemu.org

commit 0d6ee5945044d30f66553bb62457fd7b60596e46
Author: Marc-André Lureau <marcandre.lureau@redhat.com>
Date:   Mon Apr 27 15:00:46 2026 +0400

    hw/i386/pc: free pcspk on finalization

    Add pc_machine_finalize() to unref the pcspk device when it was never
    realized. Once realized, the bus owns it and no action is needed.

    Fixes: 6b8d1416482f ("audio: create pcspk device early")
    Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
    Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index 2ecad3c503..7b6ad97e5a 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -1610,6 +1610,15 @@ static void pc_machine_initfn(Object *obj)
     }
 }

+static void pc_machine_finalize(Object *obj)
+{
+    PCMachineState *pcms = PC_MACHINE(obj);
+
+    if (pcms->pcspk && !qdev_is_realized(DEVICE(pcms->pcspk))) {
+        object_unref(OBJECT(pcms->pcspk));
+    }
+}
+
 static void pc_machine_reset(MachineState *machine, ResetType type)
 {
     CPUState *cs;
@@ -1748,6 +1757,7 @@ static const TypeInfo pc_machine_info = {
     .abstract = true,
     .instance_size = sizeof(PCMachineState),
     .instance_init = pc_machine_initfn,
+    .instance_finalize = pc_machine_finalize,
     .class_size = sizeof(PCMachineClass),
     .class_init = pc_machine_class_init,
     .interfaces = (const InterfaceInfo[]) {