Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

layer: Fix dispatch table memory leak #482

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions layer/vk_layer_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) 2015-2022 Valve Corporation
* Copyright (c) 2015-2022 LunarG, Inc.
* Copyright (c) 2015-2022 Google, Inc.
* Copyright (c) 2023-2023 RasterGrid Kft.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,28 +31,26 @@ VkLayerDispatchTable *device_dispatch_table(void *object) {
dispatch_key key = get_dispatch_key(object);
device_table_map::const_iterator it = tableMap.find((void *)key);
assert(it != tableMap.end() && "Not able to find device dispatch entry");
return it->second;
return it->second.get();
}

VkLayerInstanceDispatchTable *instance_dispatch_table(void *object) {
dispatch_key key = get_dispatch_key(object);
instance_table_map::const_iterator it = tableInstanceMap.find((void *)key);
assert(it != tableInstanceMap.end() && "Not able to find instance dispatch entry");
return it->second;
return it->second.get();
}

void destroy_dispatch_table(device_table_map &map, dispatch_key key) {
device_table_map::const_iterator it = map.find((void *)key);
if (it != map.end()) {
delete it->second;
map.erase(it);
}
}

void destroy_dispatch_table(instance_table_map &map, dispatch_key key) {
instance_table_map::const_iterator it = map.find((void *)key);
if (it != map.end()) {
delete it->second;
map.erase(it);
}
}
Expand All @@ -64,14 +63,14 @@ VkLayerDispatchTable *get_dispatch_table(device_table_map &map, void *object) {
dispatch_key key = get_dispatch_key(object);
device_table_map::const_iterator it = map.find((void *)key);
assert(it != map.end() && "Not able to find device dispatch entry");
return it->second;
return it->second.get();
}

VkLayerInstanceDispatchTable *get_dispatch_table(instance_table_map &map, void *object) {
dispatch_key key = get_dispatch_key(object);
instance_table_map::const_iterator it = map.find((void *)key);
assert(it != map.end() && "Not able to find instance dispatch entry");
return it->second;
return it->second.get();
}

VkLayerInstanceCreateInfo *get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func) {
Expand Down Expand Up @@ -105,10 +104,11 @@ VkLayerInstanceDispatchTable *initInstanceTable(VkInstance instance, const PFN_v
instance_table_map::const_iterator it = map.find((void *)key);

if (it == map.end()) {
pTable = new VkLayerInstanceDispatchTable;
map[(void *)key] = pTable;
auto table = std::make_unique<VkLayerInstanceDispatchTable>();
pTable = table.get();
map[(void *)key] = std::move(table);
} else {
return it->second;
return it->second.get();
}

layer_init_instance_dispatch_table(instance, pTable, gpa);
Expand All @@ -130,10 +130,11 @@ VkLayerDispatchTable *initDeviceTable(VkDevice device, const PFN_vkGetDeviceProc
device_table_map::const_iterator it = map.find((void *)key);

if (it == map.end()) {
pTable = new VkLayerDispatchTable;
map[(void *)key] = pTable;
auto table = std::make_unique<VkLayerDispatchTable>();
pTable = table.get();
map[(void *)key] = std::move(table);
} else {
return it->second;
return it->second.get();
}

layer_init_device_dispatch_table(device, pTable, gpa);
Expand Down
6 changes: 4 additions & 2 deletions layer/vk_layer_table.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* Copyright (c) 2015-2022 The Khronos Group Inc.
* Copyright (c) 2015-2022 Valve Corporation
* Copyright (c) 2015-2022 LunarG, Inc.
* Copyright (c) 2023-2023 RasterGrid Kft.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,11 +22,12 @@

#include "vulkan/vk_layer.h"
#include "vulkan/vulkan.h"
#include <memory>
#include <unordered_map>
#include "utils/vk_layer_utils.h"

typedef std::unordered_map<void *, VkLayerDispatchTable *> device_table_map;
typedef std::unordered_map<void *, VkLayerInstanceDispatchTable *> instance_table_map;
typedef std::unordered_map<void *, std::unique_ptr<VkLayerDispatchTable>> device_table_map;
typedef std::unordered_map<void *, std::unique_ptr<VkLayerInstanceDispatchTable>> instance_table_map;
VkLayerDispatchTable *initDeviceTable(VkDevice device, const PFN_vkGetDeviceProcAddr gpa, device_table_map &map);
VkLayerDispatchTable *initDeviceTable(VkDevice device, const PFN_vkGetDeviceProcAddr gpa);
VkLayerInstanceDispatchTable *initInstanceTable(VkInstance instance, const PFN_vkGetInstanceProcAddr gpa, instance_table_map &map);
Expand Down