Basic Load Memory Leaks?

We ran asan (address sanitizer) on a simple application show below. With USD 24.08 and 24.11.

#include <fstream>
#include <iostream>
#include <string>

#include "UsdIncludeStart.hpp"
#include "pxr/base/plug/plugin.h"
#include "pxr/base/plug/registry.h"
#include "pxr/usd/usd/stage.h"
#include "UsdIncludeEnd.hpp"

#include "gtest/gtest.h"

void loadPlugins()
{
  std::string usd_path_str = <our plugin path>;
  pxr::PlugRegistry& registry = pxr::PlugRegistry::GetInstance();
  registry.RegisterPlugins(usd_path_str + "/ar/resources");
  registry.RegisterPlugins(usd_path_str + "/ndr/resources");
  registry.RegisterPlugins(usd_path_str + "/sdf/resources");
  registry.RegisterPlugins(usd_path_str + "/usd/resources");
  registry.RegisterPlugins(usd_path_str + "/usdGeom/resources");
  registry.RegisterPlugins(usd_path_str + "/usdLux/resources");
  registry.RegisterPlugins(usd_path_str + "/usdPhysics/resources");
  registry.RegisterPlugins(usd_path_str + "/usdProc/resources");
  registry.RegisterPlugins(usd_path_str + "/usdShade/resources");
  registry.RegisterPlugins(usd_path_str + "/usdSkel/resources");
  registry.RegisterPlugins(usd_path_str + "/usdUI/resources");
}

TEST(RawUSDTest, TestLoad)
{
  const char* scene_str = R"(#usda 1.0
    def Xform "hello"
    {
        def Sphere "world"
        {
        }
    }
    )";

  // Load all the plugins to registry
  loadPlugins();

  std::ofstream out;
  out.open("usd_test.usda");
  ASSERT_TRUE(out.is_open()) << "Failed to write usd string to file";
  out << scene_str << std::endl;
  out.close();

  pxr::UsdStageRefPtr stage = pxr::UsdStage::Open("usd_test.usda");
  ASSERT_NE(stage, nullptr);
}

This gives us:

=================================================================
==12==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 48 byte(s) in 2 object(s) allocated from:
    #0 0x7dabfa4b4c74 in operator new(unsigned long) (<redacted>/libasan.so.8+0xb4c74)
    #1 0x7dabf93d26e3 in pxrInternal_v0_24_11__pxrReserved__::Sdf_IdentityRegistry::Identify(pxrInternal_v0_24_11__pxrReserved__::SdfPath const&) (<redacted>/libusd_sdf.so+0x3d26e3)

SUMMARY: AddressSanitizer: 48 byte(s) leaked in 2 allocation(s).
================================================================================

Any idea as to why?

Can you try clearing the stage cache? It sometimes shows up as a leak

Not super familiar with the cache here.

Reading some documentation I would need to set one up, then clear it afterwards?

I’m guessing context is just an object to set the cache as active for the lifetime of that object? Is there some default cache?

I gave the following a try, but didn’t work.

pxr::UsdStageCache cache;
pxr::UsdStageCacheContext context(cache);

pxr::UsdStageRefPtr stage = pxr::UsdStage::Open("my file");

stageCache.Clear();

Found related Issue:

Someones fix:

I guess the real problem is no one has figured out yet why this is being held onto. We shouldn’t need a clear?

Ah yeah good find. I forgot about that.

Yes, I think its unclear what is causing it to be held on to.

Looks like removing the dead count check in pxr/usd/sdf/identity.cpp fixes it. We’re guessing the threshold is used for performance reasons. But believe this is a “safe” temporary fix.

SdfIdentities are useful for interactive authoring workflows (like in Presto), but are a notable performance drain, and yes, the deadCount is an attempt to mitigate that in some of our workflows.

We have put a fari bit of effort into avoiding the creation of Identities in USD read-only codepaths… if you can identify where your example is creating them and report it, it’s something we’d be eager to look into, rather than short-circuiting the leaky optimization. Btw, OpenUSD creates a rather substantial number of global, long-term caches that will be considered leaks…