18

In my wxWidgets application, while running in debug mode i got this message in Output of Visual Studio 2010. The application ran fine, and i only saw this after closing it.

Detected memory leaks!

Dumping objects ->

{9554} normal block at 0x003CDCC0, 44 bytes long.
Data: < e n d > 20 C1 65 01 01 00 00 00 6E 00 00 00 9C CE 64 01

{9553} normal block at 0x003CDB58, 8 bytes long.

Data: < D e < > 44 BD 65 01 C0 DC 3C 00
{9552} normal block at 0x003CDC50, 48 bytes long.

Data: < e > A0 95 65 01 01 00 00 00 19 00 00 00 19 00 00 00

Object dump complete.

In my program i am not explicitly allocating memory, however the wxWidgets framework is. I have got such a message for first time, and don't know the exact cause of it.

How can i get rid of this memory leak?

Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80
  • 1
    Post your code, preferably a minimal program which reproduces the problem. – user1071136 Dec 17 '11 at 11:14
  • Unfortunately i can't post a minimal program. Because, it is a GUI program with several files, and a GUI framework. I have no idea what is causing this. Because i have nowhere used `new` or `malloc`. – Vinayak Garg Dec 17 '11 at 11:22
  • I'm just afraid you're going to get more guesses than answers :) – user1071136 Dec 17 '11 at 11:56

5 Answers5

42

You just have to add the following lines at the beginning of your main function. Adding this flag, Visual Studio will break at the line that is creating the memory leak.

    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    _CrtSetBreakAlloc(9554);
    _CrtSetBreakAlloc(9553);
    _CrtSetBreakAlloc(9552);

Make sure you have the correct object normal block address because they might change and ensure you are compiling on _DEBUG.

See also: _CrtSetDbgFlag and _CrtSetBreakAlloc.

Nathan
  • 38
  • 5
mihaipopescu
  • 957
  • 9
  • 15
  • 1
    Thanks, What really worked for me was adding `_crtBreakAlloc = 9609;` as a very first statement. For wxWidgets I had to add it in `OnInit()` function of main application. – Vinayak Garg Dec 04 '12 at 07:32
  • 5
    Hi, how did you come up with 9609? – blue piranha Jan 10 '17 at 22:56
  • 3
    @bluepiranha That refers to the number at the beginning between the brackets. So, in his case, he had one reported at {9609}. (I know this is an old comment; this is for people like me looking for this information as well). – allen1 Jun 12 '18 at 19:40
  • Probably also worth noting that static variables using heap will show up as leaked if you call `_CrtCheckMemory()` manually, instead of using the debug flag to run it at program exit - and `_CrtSetBreakAlloc()` won't trigger for them, as they're initialised before `main()` is run. – Phi Jun 26 '20 at 19:49
0

Maybe some kinds of static instances are still allocated by the framework. Try to solve it with profiler like "devpartner".

AlexTheo
  • 4,004
  • 1
  • 21
  • 35
  • can the message from visual studio be used for getting some help? – Vinayak Garg Dec 17 '11 at 11:10
  • @VinayakGarg The can help, but to very limited use. The `Data` listed is in fact the contents of the memory address, by which you can guess what the memory is used for, and guess where it is allocated. – fefe Dec 17 '11 at 11:17
  • @CJohnson you are right he need other profiler I will change it :) thanks. – AlexTheo Dec 17 '11 at 11:39
0
  1. Never just 'assume' that your code is memory leak proof. Unless you are one of the programming demi-gods, no one is immune from possibly writing memory leaks.

  2. You could use a tool like bounds checker (From Microfocus) to help identify the memory leak because it will give you a callstack. The memory leak report you got from the debug CRT just tells you memory leaked at a particular address. A product like bounds checker will give you a callstack for that memory leak, along with lots of other goodies. There are other memory leak tools out there in the market, but I won't attempt to list them here.

  3. If you are sure the memory leak is due to 'wxWidgets', then perhaps you should inform the writers of that library and perhaps they will fix it (With suitable repro steps).

C.J.
  • 15,637
  • 9
  • 61
  • 77
  • 3
    i never said my code is memory leak proof, its just i didn't used new or malloc in my code. Yeah maybe i should try asking it on wxWidgets site. – Vinayak Garg Dec 17 '11 at 11:41
0

This wiki suggests adding the following to every source file you have, after all other header include's:

#ifdef __WXMSW__
    #include <wx/msw/msvcrt.h>      // redefines the new() operator 
#endif

This will result in leaks being reported when your program ends.

More specifically, make sure you call ->Destroy() on all objects you create using new (except maybe your top window).

user1071136
  • 15,636
  • 4
  • 42
  • 61
0

If the location of the leak reported by vs is same every time you could set a databreakpoint to see when this memory is being changed and hopefully figure out who is allocating this memory

ans
  • 1