mirror of https://github.com/auygun/kaliber.git
Fix memory leak when canceling tasks
This commit is contained in:
parent
69a05c00e9
commit
8e6589ec67
|
@ -18,16 +18,15 @@ namespace internal {
|
||||||
// one that returns via an output parameter.
|
// one that returns via an output parameter.
|
||||||
template <typename ReturnType>
|
template <typename ReturnType>
|
||||||
void ReturnAsParamAdapter(std::function<ReturnType()> func,
|
void ReturnAsParamAdapter(std::function<ReturnType()> func,
|
||||||
ReturnType* result) {
|
std::shared_ptr<ReturnType> result) {
|
||||||
*result = func();
|
*result = func();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adapts a ReturnType* result to a callblack that expects a ReturnType.
|
// Adapts a ReturnType* result to a callblack that expects a ReturnType.
|
||||||
template <typename ReturnType>
|
template <typename ReturnType>
|
||||||
void ReplyAdapter(std::function<void(ReturnType)> callback,
|
void ReplyAdapter(std::function<void(ReturnType)> callback,
|
||||||
ReturnType* result) {
|
std::shared_ptr<ReturnType> result) {
|
||||||
callback(std::move(*result));
|
callback(std::move(*result));
|
||||||
delete result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
@ -57,13 +56,20 @@ class TaskRunner {
|
||||||
std::function<ReturnType()> task,
|
std::function<ReturnType()> task,
|
||||||
std::function<void(ReturnType)> reply,
|
std::function<void(ReturnType)> reply,
|
||||||
bool front = false) {
|
bool front = false) {
|
||||||
auto* result = new ReturnType;
|
auto result = std::make_shared<ReturnType>();
|
||||||
return PostTaskAndReply(
|
return PostTaskAndReply(
|
||||||
from,
|
from,
|
||||||
std::bind(internal::ReturnAsParamAdapter<ReturnType>, std::move(task),
|
std::bind(internal::ReturnAsParamAdapter<ReturnType>, std::move(task),
|
||||||
result),
|
result),
|
||||||
std::bind(internal::ReplyAdapter<ReturnType>, std::move(reply),
|
std::bind(internal::ReplyAdapter<ReturnType>, std::move(reply), result),
|
||||||
result), front);
|
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();
|
void CancelTasks();
|
||||||
|
|
|
@ -414,10 +414,7 @@ uint64_t RendererVulkan::CreateGeometry(std::unique_ptr<Mesh> mesh) {
|
||||||
VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
|
VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
|
||||||
VK_ACCESS_TRANSFER_WRITE_BIT,
|
VK_ACCESS_TRANSFER_WRITE_BIT,
|
||||||
VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT));
|
VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT));
|
||||||
task_runner_.PostTask(HERE, [&, mesh = mesh.release()]() {
|
task_runner_.Delete(HERE, std::move(mesh));
|
||||||
// Transfer mesh ownership to the background thread.
|
|
||||||
std::unique_ptr<Mesh> own(mesh);
|
|
||||||
});
|
|
||||||
semaphore_.release();
|
semaphore_.release();
|
||||||
|
|
||||||
return last_resource_id_;
|
return last_resource_id_;
|
||||||
|
@ -506,10 +503,7 @@ void RendererVulkan::UpdateTexture(uint64_t resource_id,
|
||||||
0, VK_ACCESS_SHADER_READ_BIT,
|
0, VK_ACCESS_SHADER_READ_BIT,
|
||||||
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
||||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL));
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL));
|
||||||
task_runner_.PostTask(HERE, [&, image = image.release()]() {
|
task_runner_.Delete(HERE, std::move(image));
|
||||||
// Transfer image ownership to the background thread.
|
|
||||||
std::unique_ptr<Image> own(image);
|
|
||||||
});
|
|
||||||
semaphore_.release();
|
semaphore_.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue