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.

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.

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

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 ?

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.

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.

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.

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.

CheersDark Sylinc

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

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.

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.