Add scoped autoreleasepool to tests and examples

This ensures ObjC objects do not leak. On non-Apple
platforms, the scope does nothing.

Fixed: dawn:546
Change-Id: Id43702e6536bf9cb37825a5449511e10cf0734f5
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/55841
Commit-Queue: Austin Eng <enga@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Austin Eng
2021-06-24 19:21:31 +00:00
committed by Dawn LUCI CQ
parent b911a96fcd
commit 700a5fb869
13 changed files with 169 additions and 8 deletions

View File

@@ -71,6 +71,7 @@ static_library("dawn_utils") {
"ComboRenderPipelineDescriptor.cpp",
"ComboRenderPipelineDescriptor.h",
"PlatformDebugLogger.h",
"ScopedAutoreleasePool.h",
"SystemUtils.cpp",
"SystemUtils.h",
"TerribleCommandBuffer.cpp",
@@ -114,6 +115,12 @@ static_library("dawn_utils") {
sources += [ "PosixTimer.cpp" ]
}
if (is_mac) {
sources += [ "ScopedAutoreleasePool.mm" ]
} else {
sources += [ "ScopedAutoreleasePool.cpp" ]
}
if (dawn_supports_glfw_for_windowing) {
sources += [
"GLFWUtils.cpp",

View File

@@ -0,0 +1,34 @@
// Copyright 2021 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "utils/ScopedAutoreleasePool.h"
#include "common/Compiler.h"
namespace utils {
ScopedAutoreleasePool::ScopedAutoreleasePool() : mPool(nullptr) {
DAWN_UNUSED(mPool);
}
ScopedAutoreleasePool::~ScopedAutoreleasePool() = default;
ScopedAutoreleasePool::ScopedAutoreleasePool(ScopedAutoreleasePool&& rhs) {
}
ScopedAutoreleasePool& ScopedAutoreleasePool::operator=(ScopedAutoreleasePool&& rhs) {
return *this;
}
} // namespace utils

View File

@@ -0,0 +1,61 @@
// Copyright 2021 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef UTILS_SCOPEDAUTORELEASEPOOL_H_
#define UTILS_SCOPEDAUTORELEASEPOOL_H_
#include "common/Compiler.h"
#include <cstddef>
namespace utils {
/**
* ScopedAutoreleasePool is a scoped class which initializes an NSAutoreleasePool on
* creation, and drains it on destruction. On non-Apple platforms, ScopedAutoreleasePool
* is a no-op.
*
* An autoreleasepool is needed when using protocol objects in Objective-C because Cocoa
* expects a pool to always be available in each thread. If a pool is not available, then
* autoreleased objects will never be released and will leak.
*
* In long-running blocks of code or loops, it is important to periodically create and drain
* autorelease pools so that memory is recycled. In Dawn's tests, we have an autoreleasepool
* per-test. In graphics applications it's advised to create an autoreleasepool around the
* frame loop. Ex.)
* void frame() {
* // Any protocol objects will be reclaimed when this object falls out of scope.
* utils::ScopedAutoreleasePool pool;
*
* // do rendering ...
* }
*/
class DAWN_NO_DISCARD ScopedAutoreleasePool {
public:
ScopedAutoreleasePool();
~ScopedAutoreleasePool();
ScopedAutoreleasePool(const ScopedAutoreleasePool&) = delete;
ScopedAutoreleasePool& operator=(const ScopedAutoreleasePool&) = delete;
ScopedAutoreleasePool(ScopedAutoreleasePool&&);
ScopedAutoreleasePool& operator=(ScopedAutoreleasePool&&);
private:
void* mPool = nullptr;
};
} // namespace utils
#endif // UTILS_SCOPEDAUTORELEASEPOOL_H_

View File

@@ -0,0 +1,44 @@
// Copyright 2021 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "utils/ScopedAutoreleasePool.h"
#import <Foundation/Foundation.h>
namespace utils {
ScopedAutoreleasePool::ScopedAutoreleasePool() : mPool([[NSAutoreleasePool alloc] init]) {
}
ScopedAutoreleasePool::~ScopedAutoreleasePool() {
if (mPool != nullptr) {
[static_cast<NSAutoreleasePool*>(mPool) release];
mPool = nullptr;
}
}
ScopedAutoreleasePool::ScopedAutoreleasePool(ScopedAutoreleasePool&& rhs) {
mPool = rhs.mPool;
rhs.mPool = nullptr;
}
ScopedAutoreleasePool& ScopedAutoreleasePool::operator=(ScopedAutoreleasePool&& rhs) {
if (&rhs != this) {
mPool = rhs.mPool;
rhs.mPool = nullptr;
}
return *this;
}
} // namespace utils