(impossible?) Memory leak problem

Started by
15 comments, last by alvaro 11 years, 5 months ago
Ok so while still reducing the main function to this:
Which is basically just the main entry function for a win32 application and a return 0 at the end.
He's still giving me a memory leak output at the exact line of the "_CrtDumpMemoryLeaks();" call.
How is this even possible ? Is it a bug ? Or is windows somehow leaking memory ?
It also doesn't let me break at those allocs.


/*
****************************************************
Main.cpp - Source File
Usage: Implements Main-Entry function
****************************************************
*/

// Header for memory leak search
#ifdef MEM_DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#define _USE_MATH_DEFINES
// Includes
#include "../Framework/Engine/Core/Engine.h"
#include <cmath>

//***********************************************************
//* WinMain (int)
//* Usage: Win32 Entry-function
//* Parameters:
//* - Handle to applications instance (HINSTANCE)
//* - PreviousInstance <unused> (HINSTANCE)
//* - CommandLine string (LPSTR)
//* - CommandLine parameter (int)
//**********************************************************
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
// Search for memory leaks
#ifdef MEM_DEBUG
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF |_CRTDBG_LEAK_CHECK_DF );
_CrtDumpMemoryLeaks();
//_CrtSetBreakAlloc(147);
#endif

//// Define information of our engine
//EngineDescription engineDesc;
//// Window
//engineDesc.windowDesc.Windowed = true;
//engineDesc.windowDesc.hasBorder = false;
//engineDesc.windowDesc.windowTitle = L"DirectX11 based Engine";
//engineDesc.windowDesc.windowWidth = 1280;
//engineDesc.windowDesc.windowHeight = 720;

//// Graphics
//engineDesc.graphicsDesc.VSync = false;
//engineDesc.graphicsDesc.viewport.TopLeftX = 0.0f;
//engineDesc.graphicsDesc.viewport.TopLeftY = 0.0f;
//engineDesc.graphicsDesc.viewport.MinDepth = 0.0f;
//engineDesc.graphicsDesc.viewport.MaxDepth = 0.999f;
//engineDesc.graphicsDesc.viewport.Width = (float)engineDesc.windowDesc.windowWidth;
//engineDesc.graphicsDesc.viewport.Height = (float)engineDesc.windowDesc.windowHeight;
//engineDesc.graphicsDesc.rasterizer_state.fillMode = FM_Solid;
//engineDesc.graphicsDesc.rasterizer_state.cullMode = Cull_Back;
//
//// Global Illumination
//engineDesc.graphicsDesc.GlobalIllumination.SSAO.useSSAO = false;

//// Post Processing
//engineDesc.graphicsDesc.postFXDesc.DepthOfField = false;
//engineDesc.graphicsDesc.postFXDesc.ColorCorrection = false;
//engineDesc.graphicsDesc.postFXDesc.VolumetricScattering = false;
//engineDesc.graphicsDesc.postFXDesc.PostAA = false;
//
//
//// Camera
//engineDesc.cameraDesc.type = Free;
//engineDesc.cameraDesc.MoveSpeed = 0.5f;
//engineDesc.cameraDesc.RotationSpeed = 2.0f;
//engineDesc.cameraDesc.cameraPosition = XMFLOAT3(0.0f, -0.25f, -1.0f);
//engineDesc.cameraDesc.cameraUp = XMFLOAT3(0.0f, 1.0f, 0.0f);
//engineDesc.cameraDesc.cameraTarget = XMFLOAT3(0.0f, 0.0f, 1.0f);
//engineDesc.cameraDesc.nearClip = 0.05f;
//engineDesc.cameraDesc.farClip = 500.0f;
//engineDesc.cameraDesc.FoV = 55.0f;
//engineDesc.cameraDesc.Aspect = (float)(engineDesc.windowDesc.windowWidth / (float)engineDesc.windowDesc.windowHeight);
//


// Initialize our engine
//Engine* engine = new Engine(engineDesc);

// Start our engine
//engine->Run(new Scene(engine));

//// Release allocated memory
//if(engine)
//{
// delete engine;
// engine = NULL;
//}

return 0;
}



And the following output:

Detected memory leaks!
Dumping objects ->
{147} normal block at 0x009F5710, 80 bytes long.
Data: < W W W > 10 57 9F 00 10 57 9F 00 10 57 9F 00 CD CD CD CD
{146} normal block at 0x009F1AD0, 8 bytes long.
Data: < > D4 88 CC 01 00 00 00 00
Object dump complete.
Advertisement

Which is basically just the main entry function for a win32 application and a return 0 at the end.

Have you checked your code for static variable declarations ([s]aka [/s]globals/class variables) ? Do you clean them up ? You are including Engine.h and most likely are linking some static libs here ?
I'd suggest that you're being overly paranoid about memory leaks at too early a stage. Windows (and any other modern OS) has per-process virtual memory, and will automatically free all memory at process exit anyway, so even if _CrtDumpMemoryLeaks is reporting a leak, it's not actually a leak that you need to be too concerned about - it's going to be freed during normal app tear-down.

No, you very probably haven't found a bug in Windows that nobody else in the world is aware of. ;) Over 99% of the time when people make this diagnosis, the error is ultimately traceable to something in their own code, or a misunderstanding of how things work behind the scenes, so most likely explanation is what Ashaman73 said - your .h file is causing something to be initialized, or you have global declarations somewhere else.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


Have you checked your code for static variable declarations (aka globals ie. class variables)
[/quote]
You need to check for static variables and global variables (the two are not the same). Static variables can file file, class or function local. A global variable can be written as "int x", with the appropriate "extern" declarations visible at the point of use.

AKA, meaning "Also Known As", implies that "static" and "global" are the same. Your usage of "i.e.", which means "that is", says that all globals are class variables, which is also not true. You should use "e.g." meaning "for example".

AKA, meaning "Also Known As", implies that "static" and "global" are the same.

Yep, you are right, I was mixing the more c-like globals and static class members smile.png
Hmm well I'm not really using any static dynamically allocated variables. Some 3rd party stuff might. Even so without including anything else than <windows.h> it's still giving me the same leaks. Is this something I can ignore then?

Ashaman73: "You are including Engine.h and most likely are linking some static libs here ?"
Does that make any difference even if I'm not including it at all ?
Have you tried to start a new project, leave it completely empty and see if that has memory leaks?
There are parts of the standard library which will create static objects at the inclusion point (thus before your main function starts). This makes it possible, for example, to use std::cout without constructing a stream object manually. The standard containers will also create objects for memory allocation and such. As these objects are initialized before main, they will only be deleted after your program has ended. Therefore you cannot track memory allocation of these objects from inside your program. It has been a while, but I believe I switched to STLport at the time, which had some compiler directives to surpress this behaviour.

HTH
Well okay so I've managed to trace it back to the fbx sdk. Not sure how to proceed though. Don't think there's anything I can do.
Might be just when using its debug libraries.
This is an interesting problem with static variables. Since you call CrtDumpMemoryLeaks() before main() exits, static variables which allocate memory (which get destructed after main() exits) will still show as memory leaks. To avoid this false positive, don't call CrtDumpMemoryLeaks() manually. The _CRTDBG_LEAK_CHECK_DF flag effectively adds the memory leak dump as an atexit() call, which happens at the very end of the program.

This topic is closed to new replies.

Advertisement