diff options
author | Abseil Team <absl-team@google.com> | 2023-01-26 01:43:18 (GMT) |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-01-26 01:43:53 (GMT) |
commit | 4b4c56aff603c6a252801be71b3813b4b9eb90bb (patch) | |
tree | 033b70741e8427e08458a3dbfcc10db9e8c19a7b | |
parent | b495f72f1f096135cf9cf8c7879b5b89250de50a (diff) | |
download | googletest-4b4c56aff603c6a252801be71b3813b4b9eb90bb.zip googletest-4b4c56aff603c6a252801be71b3813b4b9eb90bb.tar.gz googletest-4b4c56aff603c6a252801be71b3813b4b9eb90bb.tar.bz2 |
Make document and example code more clear in the caveat section for potential memory leak with `SetUpTestSuite`.
PiperOrigin-RevId: 504706717
Change-Id: I5842a4569f81f6c0d636099b5cdaabe778996949
-rw-r--r-- | docs/advanced.md | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/docs/advanced.md b/docs/advanced.md index f16382f..7d15dfd 100644 --- a/docs/advanced.md +++ b/docs/advanced.md @@ -896,7 +896,8 @@ Note that `SetUpTestSuite()` may be called multiple times for a test fixture class that has derived classes, so you should not expect code in the function body to be run only once. Also, derived classes still have access to shared resources defined as static members, so careful consideration is needed when -managing shared resources to avoid memory leaks. +managing shared resources to avoid memory leaks if shared resources are not +properly cleaned up in `TearDownTestSuite()`. Here's an example of per-test-suite set-up and tear-down: @@ -907,10 +908,15 @@ class FooTest : public testing::Test { // Called before the first test in this test suite. // Can be omitted if not needed. static void SetUpTestSuite() { - // Avoid reallocating static objects if called in subclasses of FooTest. - if (shared_resource_ == nullptr) { - shared_resource_ = new ...; - } + shared_resource_ = new ...; + + // If `shared_resource_` is **not deleted** in `TearDownTestSuite()`, + // reallocation should be prevented because `SetUpTestSuite()` may be called + // in subclasses of FooTest and lead to memory leak. + // + // if (shared_resource_ == nullptr) { + // shared_resource_ = new ...; + // } } // Per-test-suite tear-down. |