Fix destruction order to avoid memory leaks reported by VMA

This commit is contained in:
Guillaume Meunier
2024-01-28 11:33:33 +01:00
parent c0c0371dbc
commit 6976281322
5 changed files with 26 additions and 13 deletions
+8 -7
View File
@@ -881,15 +881,16 @@ void application::set_wifi_locks(bool enabled)
void application::cleanup() void application::cleanup()
{ {
// Remove all scenes before destroying the allocator
scene_stack.clear();
if (allocator) if (allocator)
{ {
// vmaDestroyAllocator(allocator); vmaDestroyAllocator(allocator);
// allocator = nullptr; allocator = nullptr;
} }
#ifdef __ANDROID__ #ifdef __ANDROID__
jni::jni_thread::detach(); jni::jni_thread::detach();
#endif #endif
} }
@@ -918,10 +919,10 @@ void application::loop()
auto scene = current_scene(); auto scene = current_scene();
if (scene) if (scene)
{ {
if (scene != last_scene) if (auto tmp = last_scene.lock(); scene != tmp)
{ {
if (last_scene) if (tmp)
last_scene->on_unfocused(); tmp->on_unfocused();
scene->on_focused(); scene->on_focused();
+1 -1
View File
@@ -125,7 +125,7 @@ class application : public singleton<application>
std::mutex scene_stack_lock; std::mutex scene_stack_lock;
std::vector<std::shared_ptr<scene>> scene_stack; std::vector<std::shared_ptr<scene>> scene_stack;
std::shared_ptr<scene> last_scene; std::weak_ptr<scene> last_scene;
void loop(); void loop();
+9 -3
View File
@@ -216,13 +216,19 @@ scene_renderer::scene_renderer(vk::raii::Device & device, vk::raii::PhysicalDevi
scene_renderer::~scene_renderer() scene_renderer::~scene_renderer()
{ {
std::vector<vk::Fence> fences; wait_idle();
for (auto & i: frame_resources) }
fences.push_back(*i.fence);
void scene_renderer::wait_idle()
{
std::vector<vk::Fence> fences(frame_resources.size());
for(auto&& [i, f]: utils::enumerate(frame_resources))
fences[i] = *f.fence;
device.waitForFences(fences, true, 1'000'000'000); // TODO check for timeout device.waitForFences(fences, true, 1'000'000'000); // TODO check for timeout
} }
vk::raii::RenderPass scene_renderer::create_renderpass() vk::raii::RenderPass scene_renderer::create_renderpass()
{ {
vk::RenderPassCreateInfo info; vk::RenderPassCreateInfo info;
+3 -1
View File
@@ -148,10 +148,10 @@ public:
vk::raii::CommandBuffer cb = nullptr; vk::raii::CommandBuffer cb = nullptr;
std::vector<std::shared_ptr<void>> resources; std::vector<std::shared_ptr<void>> resources;
// Uniform buffer for per-view and per-instance data // Uniform buffer for per-view and per-instance data
// host visible, host coherent // host visible, host coherent
buffer_allocation staging_buffer; buffer_allocation staging_buffer;
// device local // device local
// buffer_allocation uniform_buffer; // buffer_allocation uniform_buffer;
}; };
@@ -181,4 +181,6 @@ public:
{ {
return default_material; return default_material;
} }
void wait_idle();
}; };
+5 -1
View File
@@ -38,7 +38,11 @@
static const std::string discover_service = "_wivrn._tcp.local."; static const std::string discover_service = "_wivrn._tcp.local.";
scenes::lobby::~lobby() {} scenes::lobby::~lobby()
{
if (renderer)
renderer->wait_idle();
}
static std::string choose_webxr_profile() static std::string choose_webxr_profile()
{ {