Memory leak? (std::map, VC7) 
Author Message
 Memory leak? (std::map, VC7)

Code:

#define WIN32_LEAN_AND_MEAN

#include <stdio.h>
#include <tchar.h>
#include <crtdbg.h>
#include <map>

int _tmain(int argc, _TCHAR* argv[])
{

 typedef std::map<int, int> INT_MAP;
 INT_MAP mm;

#ifdef _DEBUG
 _CrtDumpMemoryLeaks();
#endif

 return 0;

Quote:
}

and the output window told us:

Detected memory leaks!
Dumping objects ->
{56} normal block at 0x00373C90, 24 bytes long.
 Data: < <7  <7  <7     > 90 3C 37 00 90 3C 37 00 90 3C 37 00 CD CD CD CD
Object dump complete.

Is the above a memeory leak or a phantom.

Any ideas on how to get around these memory leaks will be appreciated.



Tue, 13 Sep 2005 13:44:09 GMT  
 Memory leak? (std::map, VC7)

Quote:

>Code:

>#define WIN32_LEAN_AND_MEAN

>#include <stdio.h>
>#include <tchar.h>
>#include <crtdbg.h>
>#include <map>

>int _tmain(int argc, _TCHAR* argv[])
>{

> typedef std::map<int, int> INT_MAP;
> INT_MAP mm;

>#ifdef _DEBUG
> _CrtDumpMemoryLeaks();
>#endif

> return 0;
>}

>and the output window told us:

>Detected memory leaks!
>Dumping objects ->
>{56} normal block at 0x00373C90, 24 bytes long.
> Data: < <7  <7  <7     > 90 3C 37 00 90 3C 37 00 90 3C 37 00 CD CD CD CD
>Object dump complete.

>Is the above a memeory leak or a phantom.

>Any ideas on how to get around these memory leaks will be appreciated.

Presumably, the leak is spurious and due to the map object, which is still
alive and well when you dump leaks. It's much more interesting to turn on
leak detect at program termination with:

   #include <crtdbg.h>

   // Insert at top of main()
   _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)
      | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF
      | _CRTDBG_DELAY_FREE_MEM_DF);
   // The following enables printing to stdout.
   _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
   _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
   _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
   _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
   _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
   _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);

This will dump leaks after main() has returned and the CRT has destroyed
static duration objects and run atexit functions.

--
Doug Harrison
Microsoft MVP - Visual C++



Tue, 13 Sep 2005 14:18:31 GMT  
 Memory leak? (std::map, VC7)

Quote:

-- snip --
> >int _tmain(int argc, _TCHAR* argv[])
> >{

> > typedef std::map<int, int> INT_MAP;
> > INT_MAP mm;

> >#ifdef _DEBUG
> > _CrtDumpMemoryLeaks();
> >#endif

> > return 0;
> >}
-- snip --
> Presumably, the leak is spurious and due to the map object, which is still
> alive and well when you dump leaks. It's much more interesting to turn on
> leak detect at program termination with:

agreed

Quote:

>    #include <crtdbg.h>

>    // Insert at top of main()
>    _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)
>       | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF
>       | _CRTDBG_DELAY_FREE_MEM_DF);
>    // The following enables printing to stdout.
>    _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
>    _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
>    _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
>    _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
>    _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
>    _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);

somewhat easier, using just to braces:

int _tmain(int argc, _TCHAR* argv[])
{

 typedef std::map<int, int> INT_MAP;

 {
  INT_MAP mm;
 }

#ifdef _DEBUG
 _CrtDumpMemoryLeaks();
#endif

 return 0;

- Show quoted text -

Quote:
}



Tue, 13 Sep 2005 22:15:46 GMT  
 Memory leak? (std::map, VC7)

Quote:

>somewhat easier, using just to braces:

>int _tmain(int argc, _TCHAR* argv[])
>{

> typedef std::map<int, int> INT_MAP;

> {
>  INT_MAP mm;
> }

>#ifdef _DEBUG
> _CrtDumpMemoryLeaks();
>#endif

> return 0;
>}

That'll work for this simple example, but dumping leaks in main() still is
not appropriate, because the CRT hasn't yet destroyed static duration
objects or called atexit functions. In real programs, you will tend to get
lots of other spurious leaks if you dump them in main(). The method I
presented in my last message avoids this problem.

--
Doug Harrison
Microsoft MVP - Visual C++



Wed, 14 Sep 2005 02:17:52 GMT  
 Memory leak? (std::map, VC7)
Thanks a lot. I will check my code according to your suggestion.

Leo Yee



Quote:

> >somewhat easier, using just to braces:

> >int _tmain(int argc, _TCHAR* argv[])
> >{

> > typedef std::map<int, int> INT_MAP;

> > {
> >  INT_MAP mm;
> > }

> >#ifdef _DEBUG
> > _CrtDumpMemoryLeaks();
> >#endif

> > return 0;
> >}

> That'll work for this simple example, but dumping leaks in main() still is
> not appropriate, because the CRT hasn't yet destroyed static duration
> objects or called atexit functions. In real programs, you will tend to get
> lots of other spurious leaks if you dump them in main(). The method I
> presented in my last message avoids this problem.

> --
> Doug Harrison
> Microsoft MVP - Visual C++



Wed, 14 Sep 2005 11:05:43 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. VC7 std::map compilation error

2. vc7 std::map compilation error

3. mem leaks when using std::map in a dll

4. Memory Leak detection in VC7

5. VC7++ - Detection of memory leaks, mixed DLL (unmanaged/managed)

6. detecting memory leaks in vc7

7. STL, std::allocator and memory mapped files

8. unresolved std::_Xran and std::_Xlen with VC7

9. Memory leak related to the std::string?

10. std::ifstream causes memory leaks !

11. Memory leak related to the std::string?

12. std::ostrstream leaking memory

 

 
Powered by phpBB® Forum Software