diff --git a/Engine/source/testing/memoryTester.h b/Engine/source/testing/memoryTester.h new file mode 100644 index 000000000..aba45f275 --- /dev/null +++ b/Engine/source/testing/memoryTester.h @@ -0,0 +1,66 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2014 GarageGames, LLC +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +//----------------------------------------------------------------------------- + +#include +#include + +#if defined(TORQUE_OS_WIN) +#define _CRTDBG_MAP_ALLOC +#include +#endif + +namespace testing +{ + // Original author: Stephan Brenner + // https://github.com/ymx/gtest_mem + class MemoryLeakDetector : public EmptyTestEventListener + { + public: + virtual void OnTestStart(const TestInfo&) + { +#if defined(TORQUE_OS_WIN) + _CrtMemCheckpoint(&memState_); +#endif + } + + virtual void OnTestEnd(const TestInfo& test_info) + { + if(test_info.result()->Passed()) + { +#if defined(TORQUE_OS_WIN) + _CrtMemState stateNow, stateDiff; + _CrtMemCheckpoint(&stateNow); + int diffResult = _CrtMemDifference(&stateDiff, &memState_, &stateNow); + if (diffResult) + { + FAIL() << "Memory leak of " << stateDiff.lSizes[1] << " byte(s) detected."; + } +#endif + } + } + + private: +#if defined(TORQUE_OS_WIN) + _CrtMemState memState_; +#endif + }; +} \ No newline at end of file diff --git a/Engine/source/testing/unitTesting.cpp b/Engine/source/testing/unitTesting.cpp index a05b93270..70d4f2b58 100644 --- a/Engine/source/testing/unitTesting.cpp +++ b/Engine/source/testing/unitTesting.cpp @@ -25,6 +25,7 @@ #include "console/engineAPI.h" #include "console/consoleInternal.h" #include "unitTesting.h" +#include "memoryTester.h" #include @@ -87,6 +88,9 @@ DefineConsoleFunction( runAllUnitTests, int, (),, // Release the default listener. delete listeners.Release( listeners.default_result_printer() ); + // Add the memory leak tester. + listeners.Append( new testing::MemoryLeakDetector ); + // Add the Torque unit test listener. listeners.Append( new TorqueUnitTestListener );