Skip to content
Saarrthi Rana

School project / Fall 2025

Memory tracker and plugins

Two engine projects in C++ for Game Engines 5.

Role
Solo
Team
Game Engines 5 course
When
Fall 2025
C++MemoryPluginsPython
The start of MemoryTracker.cpp on a dark code card
What it is

Engines need to know where their memory goes, and they often load code that isn't built into the game.

I wrote a memory tracker that counts every allocation and tells you at the end if anything leaked, and hooked it into my A* game. I also made a demo that loads a plugin while the program runs and calls its functions, from C++ and from Python.

What I did
  1. Wrote a memory tracker that counts every allocation and free, and how many bytes are still in use.
  2. Replaced C++'s new and delete so they report to the tracker while tracking is on. That keeps startup and shutdown out of the numbers.
  3. Hooked it into my A* game. It prints a summary at the end and says whether anything leaked.
  4. Made a plugin with four functions, and a program that loads it while running, finds the functions by name and calls them.
  5. Called the same plugin from Python.
How it works

What it caught

On a normal run of my A* game it counted 413 allocations and 412 frees, and flagged one 104 byte block still in use when the game closed.

Not counting itself

The tracker keeps a list of every block, and adding to that list uses memory too. A flag stops it from tracking its own allocations, which would otherwise loop forever.

Loading a plugin

The program loads the plugin file, asks for each function by name and checks it found all four before calling them. If one is missing, it reports an error instead of crashing.

Code

Code I wrote

Short parts of my own code from this project. I left out anything the course gave us.

The memory tracker

MemoryTracker.cpp, C++

The on and off switch, the flag that stops it counting itself, and my version of new. The delete side is left out.
// Guard to prevent recursion when the tracker itself allocates memory.
// Also keep a flag to tell us when tracking is active.
namespace
{
    std::atomic<bool> g_trackingEnabled{ false };
    thread_local bool g_inInternalAllocation = false;
}
// ...
void MemoryTracker::registerAllocation(void* ptr, std::size_t size)
{
    if (!g_trackingEnabled.load(std::memory_order_acquire))
        return;

    if (g_inInternalAllocation)
        return;

    g_inInternalAllocation = true;
    internalRegisterAllocation(ptr, size);
    g_inInternalAllocation = false;
}
// ...
void* operator new(std::size_t size)
{
    if (size == 0)
        size = 1;

    void* ptr = std::malloc(size);
    if (!ptr)
        throw std::bad_alloc();

    if (g_trackingEnabled.load(std::memory_order_acquire))
    {
        MemoryTracker::instance().registerAllocation(ptr, size);
    }
    return ptr;
}

Loading the plugin

main.cpp, C++

Loads the plugin, finds each function and calls them.
static std::wstring LoadPluginAndBuildOutput()
{
    std::wstringstream ss;

    // Put PluginDLL.dll next to the HostApp.exe OR copy it there as a post-build step
    std::wstring dllPath = GetExeDir() + L"PluginDLL.dll";

    gDll = LoadLibraryW(dllPath.c_str());
    if (!gDll)
    {
        DWORD err = GetLastError();
        ss << L"[ERROR] LoadLibrary failed\n";
        ss << L"Path: " << dllPath << L"\n";
        ss << L"GetLastError: " << err << L"\n";
        return ss.str();
    }

    pGetPluginName = (GetPluginNameFn)GetProcAddress(gDll, "GetPluginName");
    pAddInts = (AddIntsFn)GetProcAddress(gDll, "AddInts");
    pGetMagicNumber = (GetMagicNumberFn)GetProcAddress(gDll, "GetMagicNumber");
    pMultiply = (MultiplyFn)GetProcAddress(gDll, "Multiply");

    if (!pGetPluginName || !pAddInts || !pGetMagicNumber || !pMultiply)
    // ...
    const char* name = pGetPluginName();
    int sum = pAddInts(2, 3);
    float pi = pGetMagicNumber();
    float mul = pMultiply(2.5f, 4.0f);

    ss << L"[INFO] TOPIC 10: DLL Runtime Loading\n";
    ss << L"DLL Path: " << dllPath << L"\n\n";
    ss << L"PluginName: " << Widen(name) << L"\n";
    ss << L"AddInts(2,3) = " << sum << L"\n";
    ss << L"GetMagicNumber() = " << pi << L"\n";
    ss << L"Multiply(2.5,4.0) = " << mul << L"\n\n";
    // ...
    return ss.str();
}
Output

Program output

Console output from running the build. I cut long runs of the same line short.

// Memory tracker, at the end of a run of my A* game
==== Memory Summary ====
Total allocations:   413
Total deallocations: 412
Bytes allocated:     27200
Bytes freed:         27096
Bytes in use:        104
Live blocks:         1
Potential memory leak detected.
========================

// Plugin demo: the C++ host loads PluginDLL.dll and calls its four functions
[INFO] TOPIC 10: DLL Runtime Loading
PluginName: PluginDLL x64
AddInts(2,3) = 5
GetMagicNumber() = 3.14159
Multiply(2.5,4.0) = 10
Status: SUCCESS

// The same plugin from Python (test_plugin.py)
PluginName: PluginDLL x64
AddInts(2, 3): 5
MagicNumber: 3.141590118408203

In 2026 I made two small fixes to get both running again. The tracker had two delete functions defined twice, and the plugin needed the file name the host looks for.

Next school project

Breaking a wall with Chaos

A research project on destruction with Chaos physics in Unreal Engine 5.6.