Tutorial_TrackingMemoryLeak.cpp

Tutorial_TrackingMemoryLeak.cpp
/*
* Copyright 2016 Autodesk, Inc. All rights reserved.
* Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
* or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
*/
#include "testfwk/Fwk.h"
#include "common/basesystemenv.h"
#include "labengine/base/kaimlogimplementation.h"
// In this tutorial, we show the usage of the StackRegistry to help pin-point memory leaks in addition to the standard allocator.
// It is only active if KY_ENABLE_STACK_REGISTRY is defined.
// Look at the output to see the call-stacks of the leaking allocations.
//KT_DONT_RUN_THIS_FILE
namespace
{
#define KT_TEST_ENV_CLASS NavTest::Env
KT_TEST_ENV {}
class TraceMemoryLeak_Game
{
public:
void StartNavigation()
{
#ifdef KY_ENABLE_STACK_REGISTRY
// Enable registration of the call stack for allocations
// This allows to see the callstack which led to each allocation
bool initSymbolTable = true; // Must be done only once to let Windows load the symbols, here this is the first time.
Kaim::StackRegistry::Initialize(initSymbolTable);
#endif
config.m_defaultLogToMode = Kaim::DefaultLog::LogToMode(Kaim::DefaultLog::LogToStdout | Kaim::DefaultLog::LogToIde);
}
void ReleaseNavigation()
{
}
void Init()
{
m_world = *KY_NEW Kaim::World;
}
void DeInit_Incomplete()
{
// To show an improper DeInit, we do not clean up all ref-counted Ptr which is a common cause for memory leak.
m_world = nullptr;
// proper DeInit would also have :
// m_query = nullptr
}
void Run()
{
// Do stuff
}
~TraceMemoryLeak_Game()
{
// Only there to prevent a crash since the destructor would be called after BaseSystem is destroyed
// And all allocations and deallocation should be done between BaseSystem::Init and BaseSystem::Destroy
m_query.NullWithoutRelease();
}
Kaim::Ptr<Kaim::World> m_world;
Kaim::Ptr<Kaim::AStarQuery<Kaim::DefaultTraverseLogic> > m_query;
};
KT_TUTORIAL
{
TraceMemoryLeak_Game game;
game.StartNavigation();
game.Init();
game.Run();
game.DeInit_Incomplete();
game.ReleaseNavigation();
}
}