diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h
index c1405142f726..8bab60343aad 100644
--- a/include/libcamera/internal/media_device.h
+++ b/include/libcamera/internal/media_device.h
@@ -97,6 +97,46 @@ private:
 	unsigned int version_;
 	unsigned int hwRevision_;
 
+	class MediaV2Topology
+	{
+	public:
+		uint64_t version() const { return topology_.topology_version; }
+
+		void allocate()
+		{
+			if (!topology_.num_entities)
+				return;
+
+			clear();
+
+			ents_ = new struct media_v2_entity[topology_.num_entities]();
+			interfaces_ = new struct media_v2_interface[topology_.num_interfaces]();
+			links_ = new struct media_v2_link[topology_.num_links]();
+			pads_ = new struct media_v2_pad[topology_.num_pads]();
+
+			topology_.ptr_entities = reinterpret_cast<uintptr_t>(ents_);
+			topology_.ptr_interfaces = reinterpret_cast<uintptr_t>(interfaces_);
+			topology_.ptr_links = reinterpret_cast<uintptr_t>(links_);
+			topology_.ptr_pads = reinterpret_cast<uintptr_t>(pads_);
+		}
+
+		void clear()
+		{
+			delete[] ents_;
+			delete[] interfaces_;
+			delete[] pads_;
+			delete[] links_;
+		}
+
+		struct media_v2_topology topology_ = {};
+
+	private:
+		struct media_v2_entity *ents_ = nullptr;
+		struct media_v2_interface *interfaces_ = nullptr;
+		struct media_v2_link *links_ = nullptr;
+		struct media_v2_pad *pads_ = nullptr;
+	} mediaV2Topology_;
+
 	UniqueFD fd_;
 	bool valid_;
 	bool acquired_;
diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp
index a84bc9abc4e9..f9562aa18b80 100644
--- a/src/libcamera/media_device.cpp
+++ b/src/libcamera/media_device.cpp
@@ -195,11 +195,6 @@ void MediaDevice::unlock()
  */
 int MediaDevice::populate()
 {
-	struct media_v2_topology topology = {};
-	struct media_v2_entity *ents = nullptr;
-	struct media_v2_interface *interfaces = nullptr;
-	struct media_v2_link *links = nullptr;
-	struct media_v2_pad *pads = nullptr;
 	__u64 version = -1;
 	int ret;
 
@@ -227,13 +222,7 @@ int MediaDevice::populate()
 	 * Keep calling G_TOPOLOGY until the version number stays stable.
 	 */
 	while (true) {
-		topology.topology_version = 0;
-		topology.ptr_entities = reinterpret_cast<uintptr_t>(ents);
-		topology.ptr_interfaces = reinterpret_cast<uintptr_t>(interfaces);
-		topology.ptr_links = reinterpret_cast<uintptr_t>(links);
-		topology.ptr_pads = reinterpret_cast<uintptr_t>(pads);
-
-		ret = ioctl(fd_.get(), MEDIA_IOC_G_TOPOLOGY, &topology);
+		ret = ioctl(fd_.get(), MEDIA_IOC_G_TOPOLOGY, &mediaV2Topology_.topology_);
 		if (ret < 0) {
 			ret = -errno;
 			LOG(MediaDevice, Error)
@@ -242,37 +231,23 @@ int MediaDevice::populate()
 			goto done;
 		}
 
-		if (version == topology.topology_version)
+		if (version == mediaV2Topology_.version())
 			break;
 
-		delete[] ents;
-		delete[] interfaces;
-		delete[] pads;
-		delete[] links;
-
-		ents = new struct media_v2_entity[topology.num_entities]();
-		interfaces = new struct media_v2_interface[topology.num_interfaces]();
-		links = new struct media_v2_link[topology.num_links]();
-		pads = new struct media_v2_pad[topology.num_pads]();
-
-		version = topology.topology_version;
+		mediaV2Topology_.allocate();
+		version = mediaV2Topology_.version();
 	}
 
 	/* Populate entities, pads and links. */
-	if (populateEntities(topology) &&
-	    populatePads(topology) &&
-	    populateLinks(topology))
+	if (populateEntities(mediaV2Topology_.topology_) &&
+	    populatePads(mediaV2Topology_.topology_) &&
+	    populateLinks(mediaV2Topology_.topology_))
 		valid_ = true;
 
 	ret = 0;
 done:
 	close();
 
-	delete[] ents;
-	delete[] interfaces;
-	delete[] pads;
-	delete[] links;
-
 	if (!valid_) {
 		clear();
 		return -EINVAL;
@@ -657,6 +632,8 @@ bool MediaDevice::addObject(MediaObject *object)
  */
 void MediaDevice::clear()
 {
+	mediaV2Topology_.clear();
+
 	for (const auto &o : objects_)
 		delete o.second;
 
