From 5af0a62e5476902924df80850a178f9ec08d1b45 Mon Sep 17 00:00:00 2001 From: Zhaoming Jiang Date: Tue, 20 Jul 2021 01:17:38 +0000 Subject: [PATCH] Fix memory leak in ShaderModuleBase::MakeError Change the ShaderModuleBase::MakeError to return a Ref rather than a naked pointer. This Ref will be used and finally detached in DeviceBase::APICreateShaderModule. Bug: chromium:1223546 Change-Id: Ie4213f091e37fdc61c6e1f0bb742e8787bcb4eb8 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/58660 Reviewed-by: Corentin Wallez Reviewed-by: Austin Eng Commit-Queue: Zhaoming Jiang --- src/dawn_native/Device.cpp | 1 + src/dawn_native/ShaderModule.cpp | 4 ++-- src/dawn_native/ShaderModule.h | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/dawn_native/Device.cpp b/src/dawn_native/Device.cpp index 32c13f0d74..86434140fa 100644 --- a/src/dawn_native/Device.cpp +++ b/src/dawn_native/Device.cpp @@ -844,6 +844,7 @@ namespace dawn_native { std::unique_ptr compilationMessages( std::make_unique()); if (ConsumedError(CreateShaderModule(descriptor, compilationMessages.get()), &result)) { + DAWN_ASSERT(result == nullptr); result = ShaderModuleBase::MakeError(this); } // Move compilation messages into ShaderModuleBase and emit tint errors and warnings diff --git a/src/dawn_native/ShaderModule.cpp b/src/dawn_native/ShaderModule.cpp index bdff7a7aec..4c23874d4a 100644 --- a/src/dawn_native/ShaderModule.cpp +++ b/src/dawn_native/ShaderModule.cpp @@ -1307,8 +1307,8 @@ namespace dawn_native { } // static - ShaderModuleBase* ShaderModuleBase::MakeError(DeviceBase* device) { - return new ShaderModuleBase(device, ObjectBase::kError); + Ref ShaderModuleBase::MakeError(DeviceBase* device) { + return AcquireRef(new ShaderModuleBase(device, ObjectBase::kError)); } bool ShaderModuleBase::HasEntryPoint(const std::string& entryPoint) const { diff --git a/src/dawn_native/ShaderModule.h b/src/dawn_native/ShaderModule.h index dd3d34a993..271704295e 100644 --- a/src/dawn_native/ShaderModule.h +++ b/src/dawn_native/ShaderModule.h @@ -166,7 +166,7 @@ namespace dawn_native { ShaderModuleBase(DeviceBase* device, const ShaderModuleDescriptor* descriptor); ~ShaderModuleBase() override; - static ShaderModuleBase* MakeError(DeviceBase* device); + static Ref MakeError(DeviceBase* device); // Return true iff the program has an entrypoint called `entryPoint`. bool HasEntryPoint(const std::string& entryPoint) const;