diff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp
index 9c0ee491ab63..0be64dd836ca 100644
--- a/src/gstreamer/gstlibcamerasrc.cpp
+++ b/src/gstreamer/gstlibcamerasrc.cpp
@@ -541,9 +541,9 @@ static std::tuple<GstBufferPool *, int>
 gst_libcamera_create_video_pool(GstLibcameraSrc *self, GstPad *srcpad,
 				GstCaps *caps, const GstVideoInfo *info)
 {
-	GstQuery *query = NULL;
+	g_autoptr(GstQuery) query = NULL;
+	g_autoptr(GstBufferPool) pool = NULL;
 	const gboolean need_pool = true;
-	GstBufferPool *pool = NULL;
 
 	/*
 	 * Get the peer allocation hints to check if it supports the meta API.
@@ -587,8 +587,7 @@ gst_libcamera_create_video_pool(GstLibcameraSrc *self, GstPad *srcpad,
 		return { NULL, -EINVAL };
 	}
 
-	gst_query_unref(query);
-	return { pool, 0 };
+	return { reinterpret_cast<GstBufferPool *>(g_steal_pointer(&pool)), 0 };
 }
 
 /* Must be called with stream_lock held. */
