Fix memory leak when canceling tasks

This commit is contained in:
Attila Uygun 2023-06-24 10:01:20 +02:00
parent 69a05c00e9
commit 8e6589ec67
2 changed files with 14 additions and 14 deletions

View File

@ -18,16 +18,15 @@ namespace internal {
// one that returns via an output parameter.
template <typename ReturnType>
void ReturnAsParamAdapter(std::function<ReturnType()> func,
ReturnType* result) {
std::shared_ptr<ReturnType> result) {
*result = func();
}
// Adapts a ReturnType* result to a callblack that expects a ReturnType.
template <typename ReturnType>
void ReplyAdapter(std::function<void(ReturnType)> callback,
ReturnType* result) {
std::shared_ptr<ReturnType> result) {
callback(std::move(*result));
delete result;
}
} // namespace internal
@ -57,13 +56,20 @@ class TaskRunner {
std::function<ReturnType()> task,
std::function<void(ReturnType)> reply,
bool front = false) {
auto* result = new ReturnType;
auto result = std::make_shared<ReturnType>();
return PostTaskAndReply(
from,
std::bind(internal::ReturnAsParamAdapter<ReturnType>, std::move(task),
result),
std::bind(internal::ReplyAdapter<ReturnType>, std::move(reply),
result), front);
std::bind(internal::ReplyAdapter<ReturnType>, std::move(reply), result),
front);
}
// Posts a task to delete the given object.
template <class T>
void Delete(Location from, std::unique_ptr<T> object) {
std::shared_ptr<T> owned = std::move(object);
PostTask(HERE, [owned]() {});
}
void CancelTasks();

View File

@ -414,10 +414,7 @@ uint64_t RendererVulkan::CreateGeometry(std::unique_ptr<Mesh> mesh) {
VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
VK_ACCESS_TRANSFER_WRITE_BIT,
VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT));
task_runner_.PostTask(HERE, [&, mesh = mesh.release()]() {
// Transfer mesh ownership to the background thread.
std::unique_ptr<Mesh> own(mesh);
});
task_runner_.Delete(HERE, std::move(mesh));
semaphore_.release();
return last_resource_id_;
@ -506,10 +503,7 @@ void RendererVulkan::UpdateTexture(uint64_t resource_id,
0, VK_ACCESS_SHADER_READ_BIT,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL));
task_runner_.PostTask(HERE, [&, image = image.release()]() {
// Transfer image ownership to the background thread.
std::unique_ptr<Image> own(image);
});
task_runner_.Delete(HERE, std::move(image));
semaphore_.release();
}