Skip to content

Commit

Permalink
chassis: Fix memory leaks
Browse files Browse the repository at this point in the history
This change fixes a few memory leaks within the validation layers:

* Unused ValidationObjects created in vkCreateInstance() and
  vkCreateDevice() should be destroyed immediately.

* When modifying value of safe structs, we should reset the existing
  safe struct first.

Change-Id: Id1b1ca924e4c0ae17e9cb4b8a8ca37bfc30a9b6c
  • Loading branch information
gnoliyil authored and mark-lunarg committed Jul 10, 2020
1 parent 94f1ce3 commit 358152a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
23 changes: 23 additions & 0 deletions layers/generated/chassis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,17 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *pCreat
intercept->PostCallRecordCreateInstance(pCreateInfo, pAllocator, pInstance, result);
}

// Delete unused validation objects to avoid memory leak.
std::vector<ValidationObject*> local_objs = {
thread_checker_obj, object_tracker_obj, parameter_validation_obj,
core_checks_obj, best_practices_obj, gpu_assisted_obj, debug_printf_obj,
};
for (auto obj : local_objs) {
if (std::find(local_object_dispatch.begin(), local_object_dispatch.end(), obj) == local_object_dispatch.end()) {
delete obj;
}
}

InstanceExtensionWhitelist(framework, pCreateInfo, *pInstance);
DeactivateInstanceDebugCallbacks(report_data);
return result;
Expand Down Expand Up @@ -883,6 +894,18 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice gpu, const VkDevice
auto debug_printf_obj = new DebugPrintf;
debug_printf_obj->InitDeviceValidationObject(enables[debug_printf], instance_interceptor, device_interceptor);

// Delete unused validation objects to avoid memory leak.
std::vector<ValidationObject *> local_objs = {
thread_safety_obj, stateless_validation_obj, object_tracker_obj,
core_checks_obj, best_practices_obj, gpu_assisted_obj, debug_printf_obj,
};
for (auto obj : local_objs) {
if (std::find(device_interceptor->object_dispatch.begin(), device_interceptor->object_dispatch.end(), obj) ==
device_interceptor->object_dispatch.end()) {
delete obj;
}
}

for (auto intercept : instance_interceptor->object_dispatch) {
auto lock = intercept->write_lock();
intercept->PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
Expand Down
4 changes: 2 additions & 2 deletions layers/state_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4640,8 +4640,8 @@ void ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalD
VkPhysicalDeviceFeatures *pFeatures) {
auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
physical_device_state->vkGetPhysicalDeviceFeaturesState = QUERY_DETAILS;
physical_device_state->features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
physical_device_state->features2.pNext = nullptr;
// Reset the features2 safe struct before setting up the features field.
physical_device_state->features2 = safe_VkPhysicalDeviceFeatures2();
physical_device_state->features2.features = *pFeatures;
}

Expand Down
23 changes: 23 additions & 0 deletions scripts/layer_chassis_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,17 @@ class ValidationObject {
intercept->PostCallRecordCreateInstance(pCreateInfo, pAllocator, pInstance, result);
}
// Delete unused validation objects to avoid memory leak.
std::vector<ValidationObject*> local_objs = {
thread_checker_obj, object_tracker_obj, parameter_validation_obj,
core_checks_obj, best_practices_obj, gpu_assisted_obj, debug_printf_obj,
};
for (auto obj : local_objs) {
if (std::find(local_object_dispatch.begin(), local_object_dispatch.end(), obj) == local_object_dispatch.end()) {
delete obj;
}
}
InstanceExtensionWhitelist(framework, pCreateInfo, *pInstance);
DeactivateInstanceDebugCallbacks(report_data);
return result;
Expand Down Expand Up @@ -1514,6 +1525,18 @@ class ValidationObject {
auto debug_printf_obj = new DebugPrintf;
debug_printf_obj->InitDeviceValidationObject(enables[debug_printf], instance_interceptor, device_interceptor);
// Delete unused validation objects to avoid memory leak.
std::vector<ValidationObject *> local_objs = {
thread_safety_obj, stateless_validation_obj, object_tracker_obj,
core_checks_obj, best_practices_obj, gpu_assisted_obj, debug_printf_obj,
};
for (auto obj : local_objs) {
if (std::find(device_interceptor->object_dispatch.begin(), device_interceptor->object_dispatch.end(), obj) ==
device_interceptor->object_dispatch.end()) {
delete obj;
}
}
for (auto intercept : instance_interceptor->object_dispatch) {
auto lock = intercept->write_lock();
intercept->PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
Expand Down

0 comments on commit 358152a

Please sign in to comment.