_CrtDumpMemoryLeaks () == 1 in the first line of code?

I am working on a MFC Visual C ++ project. As I understand from MSDN , _CrtDumpMemoryLeaks() should return TRUE when there are memory leaks.

Noticing that TRUE, I tried to find the first point in the code where it will become TRUE. Obviously, this is true from the very beginning. If I press F10 (step by step) to start debugging the program and enter _CrtDumpMemoryLeaks() in the viewport, it will display TRUE even before the first line of code at the program entry point:

 extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, int nCmdShow) #pragma warning(suppress: 4985) { // call shared/exported WinMain return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow); } 

In addition, I suggested that perhaps the debugging objects are not initializing at this point and that the truth is erroneous. Therefore, I set a breakpoint on the first line in the OnInitDialog() function, and there is also a TRUE value.

Why do I have a memory leak at the beginning of the program?

+2
memory-leaks visual-c ++ mfc
source share
3 answers

You misinterpret the return value. TRUE does not mean a memory leak, it means that there are several unreleased blocks in the heap that could be noted and some pointers in the program. These objects can be created using CRT startup code and static object constructors.

If you are still suspicious - set a selection binding and check when objects are created. To do this early enough, you will need an object created at startup - use #pragma init_seg( compiler ) to do this.

+3
source share

C ++ initializes static objects before calling the main () method (or WinMain, if this is the case).

Do you have static objects? Are you using Singleton to initialize any object that has never been freed? Are you using some framework that could do this behind your back?

+2
source share

If you have things that allocate memory during static initialization (what happens before winmain is called), then they will appear as a memory leak.

You can try to set a breakpoint on HeapAlloc () - you will probably see what it gets before winmain does.

+1
source share

All Articles