Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


(impossible?) Memory leak problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 lipsryme   Members   -  Reputation: 1052

Like
0Likes
Like

Posted 12 November 2012 - 06:19 AM

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.

Edited by lipsryme, 12 November 2012 - 06:32 AM.


Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 8001

Like
2Likes
Like

Posted 12 November 2012 - 06:43 AM

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 (aka globals/class variables) ? Do you clean them up ? You are including Engine.h and most likely are linking some static libs here ?

Edited by Ashaman73, 12 November 2012 - 07:13 AM.


#3 mhagain   Crossbones+   -  Reputation: 8284

Like
1Likes
Like

Posted 12 November 2012 - 07:01 AM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#4 rip-off   Moderators   -  Reputation: 8762

Like
2Likes
Like

Posted 12 November 2012 - 07:04 AM

Have you checked your code for static variable declarations (aka globals ie. class variables)

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".

#5 Ashaman73   Crossbones+   -  Reputation: 8001

Like
0Likes
Like

Posted 12 November 2012 - 07:12 AM

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 Posted Image

#6 lipsryme   Members   -  Reputation: 1052

Like
0Likes
Like

Posted 12 November 2012 - 08:30 AM

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 ?

Edited by lipsryme, 12 November 2012 - 08:32 AM.


#7 Álvaro   Crossbones+   -  Reputation: 13933

Like
0Likes
Like

Posted 12 November 2012 - 09:00 AM

Have you tried to start a new project, leave it completely empty and see if that has memory leaks?

#8 Prototype   Members   -  Reputation: 1436

Like
0Likes
Like

Posted 12 November 2012 - 09:21 AM

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

#9 lipsryme   Members   -  Reputation: 1052

Like
0Likes
Like

Posted 12 November 2012 - 02:25 PM

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.

#10 Qanael   Members   -  Reputation: 130

Like
2Likes
Like

Posted 12 November 2012 - 08:39 PM

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.

Edited by Qanael, 12 November 2012 - 08:40 PM.


#11 Khatharr   Crossbones+   -  Reputation: 3069

Like
1Likes
Like

Posted 13 November 2012 - 04:23 PM

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".


First smirk of the day award goes to rip-off.

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. I was about to look up the function there because I suspected that this was the case.

Memory leak detectors work by reporting on anything that's still allocated when they are invoked (they hook the allocation and deallocation functions to keep track). So if you're calling the function prior to the end of the program then anything that's still allocated will be reported on.

I'd also recommend checking out VLD. Someone here suggested it to me and I tried it and love it. You can just double-click the message in the report to go straight to the leaky allocation in the code.

Edited by Khatharr, 13 November 2012 - 04:28 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#12 lipsryme   Members   -  Reputation: 1052

Like
0Likes
Like

Posted 14 November 2012 - 07:00 AM

Thanks guys. Good to know :)

Edited by lipsryme, 14 November 2012 - 07:01 AM.


#13 Matias Goldberg   Crossbones+   -  Reputation: 3722

Like
0Likes
Like

Posted 14 November 2012 - 10:53 AM

No one really saw it? Really???

There's a lot that goes on after leaving main(); hence why it's telling you there are leaks.
Try using atexit.

Personally, I prefer to create a static object, ensure it's the first one to get created, and then let the C++ compiler clean up last:

[source lang="cpp"]class MeFirst{ MeFirst() {} ~MeFirst() { /* -- call CRT debug dump here -- */ } static MeFirst&amp; getSingleton() { static MeFirst instance; return instance; }}int main(){ //Calling first may ensure the static initialization goes first. volatile MeFirst &amp;doThisFirst = MeFirst::getSingleton(); return 0;}[/source]

It's out of my head, and beware I may be missing a case where another static variable gets initialized before this one, hence you may still get leaks (before it will be destroyed after CRT dump). Anyway, try different approaches, google a little, experiment, and find out how to make the CRT Dump get truly called last.
wxWidgets manages to do it correctly, you may want to take a look there.

Cheers
Dark Sylinc

Edit: Oh, and Singleton is an evil pattern. Don't use it. This is an exception because you're not actually using it.

Edited by Matias Goldberg, 14 November 2012 - 10:54 AM.


#14 Khatharr   Crossbones+   -  Reputation: 3069

Like
0Likes
Like

Posted 14 November 2012 - 06:31 PM

Why would you use a singleton to implement something that doesn't need implemented?

The _CRTDBG_LEAK_CHECK_DF flag effectively adds the memory leak dump as an atexit() call,


Additionally, there's absolutely no guarantee that instantiating 'first' will make that object deconstruct last. Additionally, if he's getting leaks from just including a header file then obviously he's got something doing allocations prior to main(), so being 'first' by instantiating in main isn't possible and there's explicitly no defined order for global or static initializations across compilation units.

Edited by Khatharr, 14 November 2012 - 06:33 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#15 Álvaro   Crossbones+   -  Reputation: 13933

Like
1Likes
Like

Posted 14 November 2012 - 07:11 PM

Additionally, there's absolutely no guarantee that instantiating 'first' will make that object deconstruct last.


Actually, I am pretty sure there is a guarantee that destruction of static objects will happen in the reverse order of construction (more precisely, the end of construction).

#16 Khatharr   Crossbones+   -  Reputation: 3069

Like
0Likes
Like

Posted 14 November 2012 - 09:34 PM

I misspoke. I meant in the context of the 'pre-main' objects.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#17 Álvaro   Crossbones+   -  Reputation: 13933

Like
0Likes
Like

Posted 15 November 2012 - 02:01 AM

I misspoke. I meant in the context of the 'pre-main' objects.

I am not sure if you misspoke or you misunderstood: The static objects I am referring to are meant to contain what you call "pre-main" objects.

If I remember the rules correctly, within a translation unit (a .cpp file), global objects are constructed in the order they are defined, but there is no guarantee of what order things in different translation units will be constructed (which means there is no solution that guarantees your object will be "first", unless it's something specific the compiler provides). Static local variables are constructed on the first function call where they are needed (which could be as part of the construction of some other static object). Then destructors will be invoked in the reverse order of when the constructors finished.

As an example of how these guarantees can be useful, think of a program that has a singleton implementation of a log. If a global object wants to write something out to the log at destruction (statistics of usage or what have you), you need to make sure that the log gets destructed later than the global object. The global object can guarantee this by using the log from the constructor (all you need is something like Log::get_instance(), even if you do nothing with it). Then the log's constructor is guaranteed to have finished before the object's constructor, and destructors will be called in the right order.

DISCLAIMER: After one attempt at trying to be clever about these things, I decided that the advice to not use global variables of any type is golden, and I strongly discourage people from doing what I described in the paragraph above. Don't use global variables, or singletons, or static local variables, or static member data, and you'll save yourself some really bad headaches.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS