I’m building a video game engine using C++ as the coding language and Vulkan for…
- User
I’m building a video game engine using C++ as the coding language and Vulkan for graphics. I am trying to set up a generic renderer using Vulkan that is flexible and will render objects based on a vector that is supplied to it. The renderer will also handle the creation of the window using GLFW and use GLM for all relevant math calls. I am using the ASSIMP library to load 3d models and animations.
Here is a portion of the code:
Engine.h:
#pragma once
#include "Window.h" #include "Renderer.h" #include "Scene.h"
class Engine { public: Engine(); ~Engine();
void Run(); void Shutdown();private:
void Initialize(); void MainLoop(); void Update(float deltaTime); void Render(); Window window; Renderer renderer; Scene scene;};
Engine.cpp:
#include "Engine.h" #include "Terrain.h" #include
Engine::Engine() { Initialize(); }
Engine::~Engine() { Shutdown(); }
void Engine::Run() { MainLoop(); }
void Engine::Initialize() { // Initialize window, renderer, and scene window.Initialize(); renderer.Initialize(window.GetWindow()); scene.Initialize();
VkDescriptorSetLayout descriptorSetLayout = renderer.CreateDescriptorSetLayout(); VkDescriptorPool descriptorPool = renderer.CreateDescriptorPool(1); // Assuming only one terrain object Terrain terrain(0,10,1,renderer.GetDevice(), renderer.GetPhysicalDevice(), renderer.GetCommandPool(), renderer.GetGraphicsQueue()); terrain.GenerateTerrain(descriptorSetLayout, descriptorPool); scene.AddGameObject(terrain.GetTerrainObject()); float deltaTime = window.GetDeltaTime();}
void Engine::MainLoop() { while (!window.ShouldClose()) { window.PollEvents();
float deltaTime = window.GetDeltaTime(); Update(deltaTime); Render(); }}
void Engine::Update(float deltaTime) { scene.Update(deltaTime); }
void Engine::Render() { renderer.BeginFrame(); scene.Render(renderer); renderer.EndFrame(); }
void Engine::Shutdown() { // Clean up resources in reverse order scene.Shutdown(); renderer.Shutdown(); window.Shutdown(); }
Can you please check the code for any potential memory leaks? If you find any, please tell me the changes required to fix them.
- Assistant
It seems that most of the memory management is handled correctly in the provided code. However, there are no comments or documentation for the included libraries and some of the Vulkan helper functions. I will point out a potential memory leak with the descriptor set and pool creation, and recommend some changes to clean up.
Potential memory leak:
In the
Initialize()function of yourEngineclass, you create a descriptor set layout and a descriptor pool. However, in theShutdown()function, you don't destroy them. To fix the memory leaks and ensure proper cleanup, you should store the generated VkDescriptorSetLayout and VkDescriptorPool as class members in the Engine class and destroy them in theEngine::Shutdown()function.Changes required:
- In your
Engine.hfile, add the following private member variables:
VkDescriptorSetLayout descriptorSetLayout; VkDescriptorPool descriptorPool;- In your
Engine.cppfile, modify theInitialize()method to remove the local variable declarations fordescriptorSetLayoutanddescriptorPool, and assign values directly to the class member variables:
void Engine::Initialize() { // (Your existing code here ...) descriptorSetLayout = renderer.CreateDescriptorSetLayout(); descriptorPool = renderer.CreateDescriptorPool(1); // (The rest of the code ...) }- In your
Engine.cppfile, add the code to destroy the descriptor set layout and pool in theShutdown()method:
void Engine::Shutdown() { // Clean up resources in reverse order scene.Shutdown(); renderer.Shutdown(); // Destroy descriptor set layout and pool vkDestroyDescriptorPool(renderer.GetDevice(), descriptorPool, nullptr); vkDestroyDescriptorSetLayout(renderer.GetDevice(), descriptorSetLayout, nullptr); window.Shutdown(); }With these changes, your
Engineclass should properly manage the memory for descriptor set layout and descriptor pool. - In your
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.