Possible memory leaks (detected by Dr.Memrory)

Discussion area about developing with Ogre-Next (2.1, 2.2 and beyond)


knn217
Halfling
Posts: 78
Joined: Wed Jan 25, 2023 9:04 am
x 5

Possible memory leaks (detected by Dr.Memrory)

Post by knn217 »

Hello, I've been refactoring my project and saw some leaks detected when I run Dr.Memory (as a Visual Studio tool).
Here's the results from Dr.Memory:

Code: Select all

Error #52: LEAK 64 bytes 
replace_malloc 
    D:\a\drmemory\drmemory\common\alloc_replace.c(2580):
RenderSystem_GL3Plus_d.dll!Ogre::CPreprocessor::Token::Append
    D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLPreprocessor.cpp(91):
RenderSystem_GL3Plus_d.dll!Ogre::CPreprocessor::Parse
    D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLPreprocessor.cpp(1431):
RenderSystem_GL3Plus_d.dll!Ogre::GLSLShader::loadFromSource
    D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLShader.cpp(183):
OgreNextMain_d.dll!Ogre::HighLevelGpuProgram::loadHighLevelImpl
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHighLevelGpuProgram.cpp(329):
OgreNextMain_d.dll!Ogre::HighLevelGpuProgram::loadHighLevel
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHighLevelGpuProgram.cpp(184):
OgreNextMain_d.dll!Ogre::HighLevelGpuProgram::createParameters
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHighLevelGpuProgram.cpp(153):
RenderSystem_GL3Plus_d.dll!Ogre::GLSLShader::createParameters
    D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLShader.cpp(541):
OgreNextMain_d.dll!Ogre::GpuProgram::getDefaultParameters
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreGpuProgram.cpp(409):
OgreNextMain_d.dll!Ogre::GpuProgramTranslator::translate
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreScriptTranslator.cpp(4698):
OgreNextMain_d.dll!Ogre::ScriptCompiler::compile
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreScriptCompiler.cpp(353):
OgreNextMain_d.dll!Ogre::ScriptCompiler::compile
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreScriptCompiler.cpp(259):

Error #53: LEAK 64 bytes 
replace_malloc 
    D:\a\drmemory\drmemory\common\alloc_replace.c(2580):
RenderSystem_GL3Plus_d.dll!Ogre::CPreprocessor::Token::Append
    D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLPreprocessor.cpp(91):
RenderSystem_GL3Plus_d.dll!Ogre::CPreprocessor::Parse
    D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLPreprocessor.cpp(1431):
RenderSystem_GL3Plus_d.dll!Ogre::GLSLShader::loadFromSource
    D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLShader.cpp(183):
OgreNextMain_d.dll!Ogre::HighLevelGpuProgram::loadHighLevelImpl
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHighLevelGpuProgram.cpp(329):
OgreNextMain_d.dll!Ogre::HighLevelGpuProgram::loadHighLevel
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHighLevelGpuProgram.cpp(184):
OgreNextMain_d.dll!Ogre::HighLevelGpuProgram::createParameters
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHighLevelGpuProgram.cpp(153):
RenderSystem_GL3Plus_d.dll!Ogre::GLSLShader::createParameters
    D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLShader.cpp(541):
OgreNextMain_d.dll!Ogre::GpuProgram::getDefaultParameters
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreGpuProgram.cpp(409):
OgreNextMain_d.dll!Ogre::GpuProgramTranslator::translate
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreScriptTranslator.cpp(4696):
OgreNextMain_d.dll!Ogre::ScriptCompiler::compile
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreScriptCompiler.cpp(353):
OgreNextMain_d.dll!Ogre::ScriptCompiler::compile
    D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreScriptCompiler.cpp(259):

Error #54: LEAK 8 bytes 
replace_operator_new
    D:\a\drmemory\drmemory\common\alloc_replace.c(2903):
RenderSystem_Direct3D11_d.dll!Ogre::D3D11VendorExtension::initializeExtension
    D:\OGRE_NEXT\ogre-next\RenderSystems\Direct3D11\src\VendorExtensions\OgreD3D11VendorExtension.cpp(58):
RenderSystem_Direct3D11_d.dll!Ogre::D3D11RenderSystem::D3D11RenderSystem
    D:\OGRE_NEXT\ogre-next\RenderSystems\Direct3D11\src\OgreD3D11RenderSystem.cpp(135):
RenderSystem_Direct3D11_d.dll!Ogre::D3D11Plugin::install   
D:\OGRE_NEXT\ogre-next\RenderSystems\Direct3D11\src\OgreD3D11Plugin.cpp(44): OgreNextMain_d.dll!Ogre::Root::installPlugin D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreRoot.cpp(1426): RenderSystem_Direct3D11_d.dll!dllStartPlugin
D:\OGRE_NEXT\ogre-next\RenderSystems\Direct3D11\src\OgreD3D11EngineDll.cpp(40): DerivedGraphicsSystem::initialize D:\OGRE_NEXT\game\src\OgreDerived\DerivedGraphicsSystem.cpp(58): WinMain D:\OGRE_NEXT\game\src\main.cpp(72): Error #55: LEAK 64 bytes replace_malloc D:\a\drmemory\drmemory\common\alloc_replace.c(2580): RenderSystem_GL3Plus_d.dll!Ogre::CPreprocessor::Token::Append D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLPreprocessor.cpp(91): RenderSystem_GL3Plus_d.dll!Ogre::CPreprocessor::Parse D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLPreprocessor.cpp(1431): RenderSystem_GL3Plus_d.dll!Ogre::GLSLShader::loadFromSource D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLShader.cpp(183): OgreNextMain_d.dll!Ogre::HighLevelGpuProgram::loadHighLevelImpl D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHighLevelGpuProgram.cpp(329): OgreNextMain_d.dll!Ogre::HighLevelGpuProgram::loadHighLevel D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHighLevelGpuProgram.cpp(184): OgreNextMain_d.dll!Ogre::HighLevelGpuProgram::createParameters D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHighLevelGpuProgram.cpp(153): RenderSystem_GL3Plus_d.dll!Ogre::GLSLShader::createParameters D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLShader.cpp(541): OgreNextMain_d.dll!Ogre::UnifiedHighLevelGpuProgram::createParameters D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreUnifiedHighLevelGpuProgram.cpp(153): OgreNextMain_d.dll!Ogre::GpuProgramUsage::recreateParameters D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreGpuProgramUsage.cpp(230): OgreNextMain_d.dll!Ogre::GpuProgramUsage::setProgramName D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreGpuProgramUsage.cpp(105): OgreNextMain_d.dll!Ogre::Pass::setFragmentProgram D:\OGRE_NEXT\ogre-next\OgreMain\src\OgrePass.cpp(846): Error #56: LEAK 64 bytes replace_malloc D:\a\drmemory\drmemory\common\alloc_replace.c(2580): RenderSystem_GL3Plus_d.dll!Ogre::CPreprocessor::Token::Append D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLPreprocessor.cpp(91): RenderSystem_GL3Plus_d.dll!Ogre::CPreprocessor::Parse D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLPreprocessor.cpp(1431): RenderSystem_GL3Plus_d.dll!Ogre::GLSLShader::loadFromSource D:\OGRE_NEXT\ogre-next\RenderSystems\GL3Plus\src\GLSL\OgreGLSLShader.cpp(183): OgreNextMain_d.dll!Ogre::HighLevelGpuProgram::loadHighLevelImpl D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHighLevelGpuProgram.cpp(329): OgreNextMain_d.dll!Ogre::HighLevelGpuProgram::loadHighLevel D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHighLevelGpuProgram.cpp(184): OgreNextMain_d.dll!Ogre::HighLevelGpuProgram::loadImpl D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHighLevelGpuProgram.cpp(113): OgreNextMain_d.dll!Ogre::Hlms::compileShaderCode D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHlms.cpp(2244): OgreNextMain_d.dll!Ogre::Hlms::_compileShaderFromPreprocessedSource D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHlms.cpp(2279): OgreNextMain_d.dll!Ogre::HlmsDiskCache::_compileShadersThread D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHlmsDiskCache.cpp(203): OgreNextMain_d.dll!Ogre::HlmsDiskCache::applyTo D:\OGRE_NEXT\ogre-next\OgreMain\src\OgreHlmsDiskCache.cpp(307): Demo::GraphicsSystem::loadHlmsDiskCache D:\OGRE_NEXT\ogre-next\Samples\2.0\Common\src\GraphicsSystem.cpp(637): =========================================================================== FINAL SUMMARY: DUPLICATE ERROR COUNTS: Error # 3: 1690 Error # 4: 1937 Error # 5: 422 Error # 6: 1690 Error # 7: 1937 Error # 8: 422 Error # 12: 2 Error # 13: 3 Error # 14: 2 Error # 15: 3 Error # 16: 2 Error # 17: 3 Error # 18: 2 Error # 19: 3 Error # 20: 2 Error # 21: 3 Error # 22: 2 Error # 23: 3 Error # 26: 3 Error # 28: 3 Error # 30: 3 Error # 32: 3 Error # 34: 3 Error # 36: 3 Error # 38: 3 Error # 40: 3 Error # 42: 3 Error # 44: 3 Error # 46: 3 Error # 48: 3 Error # 49: 118 Error # 50: 8 Error # 51: 5 Error # 52: 13 Error # 53: 5 Error # 55: 3 Error # 56: 2 SUPPRESSIONS USED: ERRORS FOUND: 0 unique, 0 total unaddressable access(es) 51 unique, 8313 total uninitialized access(es) 0 unique, 0 total invalid heap argument(s) 0 unique, 0 total GDI usage error(s) 0 unique, 0 total handle leak(s) 0 unique, 0 total warning(s) 5 unique, 24 total, 1480 byte(s) of leak(s) 0 unique, 0 total, 0 byte(s) of possible leak(s)

This could be false positive but I'm reporting it here just in case.

My program uses a derived class from GraphicsSystem, which initialize differently so that I can pass in an external window handle in main function. Here's DerivedGraphicsSystem initialize and deinitialize:

Code: Select all

void DerivedGraphicsSystem::initialize(
    HWND& hwnd, 
    const char* pluginsFile, 
    int& width, int& height, 
    bool& fullscreen
    )
{
    const Ogre::String windowTitle = "Window's Title";
    mWinHandle = hwnd;

const Ogre::AbiCookie abiCookie = Ogre::generateAbiCookie();
mRoot = OGRE_NEW Ogre::Root( &abiCookie, 
    mPluginsFolder + pluginsFile, 
    mWriteAccessFolder + "ogre.cfg",           
    mWriteAccessFolder + "Ogre.log",
    windowTitle);

mStaticPluginLoader.install( mRoot );

// enable sRGB Gamma Conversion mode by default for all renderers,
// but still allow to override it via config dialog
Ogre::RenderSystemList::const_iterator itor = mRoot->getAvailableRenderers().begin();
Ogre::RenderSystemList::const_iterator endt = mRoot->getAvailableRenderers().end();

while( itor != endt )
{
    Ogre::RenderSystem *rs = *itor;
    rs->setConfigOption( "sRGB Gamma Conversion", "Yes" );
    ++itor;
}

//mAlwaysAskForConfig = false;
if( mAlwaysAskForConfig || !mRoot->restoreConfig() )
{
    if( !mRoot->showConfigDialog() )
    {
        mQuit = true;
        return;
    }
}

mRoot->initialise( false );

Ogre::ConfigOptionMap &cfgOpts = mRoot->getRenderSystem()->getConfigOptions();

Ogre::ConfigOptionMap::iterator opt = cfgOpts.find( "Video Mode" );
if( opt != cfgOpts.end() && !opt->second.currentValue.empty() )
{
    // Ignore leading space
    const Ogre::String::size_type start = opt->second.currentValue.find_first_of( "012356789" );
    // Get the width and height
    Ogre::String::size_type widthEnd = opt->second.currentValue.find( ' ', start );
    // we know that the height starts 3 characters after the width and goes until the next space
    Ogre::String::size_type heightEnd = opt->second.currentValue.find( ' ', widthEnd + 3 );
    // Now we can parse out the values
    width = Ogre::StringConverter::parseInt( opt->second.currentValue.substr( 0, widthEnd ) );
    height = Ogre::StringConverter::parseInt( opt->second.currentValue.substr( widthEnd + 3, heightEnd ) );
}
fullscreen = Ogre::StringConverter::parseBool( cfgOpts["Full Screen"].currentValue );

Ogre::NameValuePairList params;

// insert external window handle (hwnd) and set the window's position
params.insert(std::make_pair("externalWindowHandle", Ogre::StringConverter::toString((unsigned long long)mWinHandle)));
SetWindowPos(mWinHandle, HWND_TOP, 0, 0, width, height, 0);
//-----------------------
params.insert( std::make_pair( "title", "" ) );
params.insert( std::make_pair( "gamma", cfgOpts["sRGB Gamma Conversion"].currentValue ) );
if( cfgOpts.find( "VSync Method" ) != cfgOpts.end() )
    params.insert( std::make_pair( "vsync_method", cfgOpts["VSync Method"].currentValue ) );
params.insert( std::make_pair( "FSAA", cfgOpts["FSAA"].currentValue ) );
params.insert( std::make_pair( "vsync", cfgOpts["VSync"].currentValue ) );
params.insert( std::make_pair( "reverse_depth", "Yes" ) );


initMiscParamsListener( params );

mRenderWindow = Ogre::Root::getSingleton().createRenderWindow(
    "Main RenderWindow",                                              
    static_cast<uint32_t>( width ), static_cast<uint32_t>( height ),  
    fullscreen, &params );

mOverlaySystem = OGRE_NEW Ogre::v1::OverlaySystem();

setupResources();
loadResources();
chooseSceneManager();
createCamera();
mWorkspace = setupCompositor();
}
//---------------------------------------------------------------------------------------
void DerivedGraphicsSystem::deinitialize()
{
    saveTextureCache();
    saveHlmsDiskCache();

if (mSceneManager)
{
    Ogre::AtmosphereComponent* atmosphere = mSceneManager->getAtmosphereRaw();
    OGRE_DELETE atmosphere;

    mSceneManager->removeRenderQueueListener(mOverlaySystem);
}

OGRE_DELETE mOverlaySystem;
mOverlaySystem = 0;

OGRE_DELETE mRoot;
mRoot = 0;

}
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5433
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1341

Re: Possible memory leaks (detected by Dr.Memrory)

Post by dark_sylinc »

This leak looks legit. Thanks for reporting it!

Do you happen to have the shader source (and are you able to share it?) so it's easier to repro?

Cheers
Matias

knn217
Halfling
Posts: 78
Joined: Wed Jan 25, 2023 9:04 am
x 5

Re: Possible memory leaks (detected by Dr.Memrory)

Post by knn217 »

dark_sylinc wrote: Wed May 01, 2024 4:10 pm

Do you happen to have the shader source (and are you able to share it?) so it's easier to repro?

Sure, here's all the shaders that the project generated: https://drive.google.com/file/d/16mxC5H ... sp=sharing

The program is still simple, only create a window and render to it with ogre, so there's not that many shaders.

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5433
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1341

Re: Possible memory leaks (detected by Dr.Memrory)

Post by dark_sylinc »

Fixed! Thanks!

knn217
Halfling
Posts: 78
Joined: Wed Jan 25, 2023 9:04 am
x 5

Re: Possible memory leaks (detected by Dr.Memrory)

Post by knn217 »

I found some more leaks, it seems DrMemory has a limit for reporting leaks (which can be turned off with a command line arg but it did't work for me).
Anyways, here are the leak folders: https://drive.google.com/file/d/1bv12Xf ... sp=sharing

I could only test 2 cases:
_Quit at config window
_Render with opengl (I tried with Direct3d11 and Vulkan too but they crashed)

It's probably better if you run DrMemory on other projects you can think of that might have leaks, since mine is still a primitive program, which might not hit a lot of leak cases.

Also, my application has a problem if picked Vulkan (it's fine with opengl and direct3d11), where it throws when I try to close or minimize the window to the taskbar. Dunno if it's because I'm rendering to an external hwnd but the problem was something about swapchain, I put a screenshot of the call stack in the link above.

knn217
Halfling
Posts: 78
Joined: Wed Jan 25, 2023 9:04 am
x 5

Re: Possible memory leaks (detected by Dr.Memrory)

Post by knn217 »

Installing and using "edherbert/ogre-next-imgui" somehow fixes the swapchain assertion for dragging the window while using Vulkan, but the assertion still happens when minimizing the window, at this line:

Code: Select all

VkSemaphore VulkanWindowSwapChainBased::getImageAcquiredSemaphore()
{
    OGRE_ASSERT_LOW( mSwapchainStatus != SwapchainReleased );
    ...
}

OpenGL and Direct3D11 are still fine though.
I still want to use Vulkan so my fix right now is to create a WM_POPUP instead of WM_OVERLAPPEDWINDOW.

About the leaks, I'm not sure if my reports are false positive or not, but given the 1st report was legit, I'll keep running DrMemory as my project gets more comlex and post the result here if it leaks from Ogre.

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5433
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1341

Re: Possible memory leaks (detected by Dr.Memrory)

Post by dark_sylinc »

Hi!

Does the assertion happen on the samples?
What GPU do you have and Windows 10 or 11?

knn217
Halfling
Posts: 78
Joined: Wed Jan 25, 2023 9:04 am
x 5

Re: Possible memory leaks (detected by Dr.Memrory)

Post by knn217 »

Hello, thanks for the reply!

dark_sylinc wrote: Sun May 12, 2024 4:14 pm

Does the assertion happen on the samples?

Wait, now that I tested with the samples, it seems they also have the assertion when minimizing too (I tested with tutorial 00, 01 and 06). I also tried dragging the window, resizing(by clicking the resize button and dragging the edges), closing with "x" button and alt+f4, these are all fine. Only minimizing to the taskbar cause the assertion.

dark_sylinc wrote: Sun May 12, 2024 4:14 pm

What GPU do you have and Windows 10 or 11?

I'm using Windows 11, when running Ogre apps, I have 3 choices for Vulkan:
_default
_NVIDIA GeForce RTX 3050
_AMD Radeon(TM)
(Direct3d11 also have _Microsoft basic render driver)

I tested with all these options and they all have this assertion, only for minimizing to taskbar.