summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2021-09-15 23:27:09 -0400
committerDerek Mauro <dmauro@google.com>2021-09-16 13:00:01 -0400
commit6bfab0beccaab26d8d3762912b7ccda66e6281ef (patch)
tree14b63c79e50ffb7e926a4f71d5d16215ea1cd036
parent8aa657ee397a3273f9d2bb14a10b39fd1d556a77 (diff)
downloadgoogletest-git-6bfab0beccaab26d8d3762912b7ccda66e6281ef.tar.gz
Googletest export
Add a caveat section for potential memory leak with derived classes using `SetUpTestSuite`. PiperOrigin-RevId: 396986728
-rw-r--r--docs/advanced.md11
1 files changed, 10 insertions, 1 deletions
diff --git a/docs/advanced.md b/docs/advanced.md
index 620180fb..c71ba413 100644
--- a/docs/advanced.md
+++ b/docs/advanced.md
@@ -887,6 +887,12 @@ preceding or following another. Also, the tests must either not modify the state
of any shared resource, or, if they do modify the state, they must restore the
state to its original value before passing control to the next test.
+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.
+
Here's an example of per-test-suite set-up and tear-down:
```c++
@@ -896,7 +902,10 @@ class FooTest : public testing::Test {
// Called before the first test in this test suite.
// Can be omitted if not needed.
static void SetUpTestSuite() {
- shared_resource_ = new ...;
+ // Avoid reallocating static objects if called in subclasses of FooTest.
+ if (shared_resource_ == nullptr) {
+ shared_resource_ = new ...;
+ }
}
// Per-test-suite tear-down.